120 likes | 250 Views
Lecture 8: JavaScript. JavaScript Syntax, how to use it in a HTML document. What is JavaScript. JavaScript is a programming language that can be executed in almost every modern browser. It is different from traditional programming language such as C++, Java.
E N D
Lecture 8: JavaScript JavaScript Syntax, how to use it in a HTML document
What is JavaScript • JavaScript is a programming language that can be executed in almost every modern browser. • It is different from traditional programming language such as C++, Java. • It is a interpreted or scripting language. • Do not need to be compiled before execution. • Must be executed by an interpreter. A browser contains a JavaScript interpreter.
What is JavaScript • JavaScript is client-side programming language. • It is executed in the browser of a client, not on a web server. • It allows interaction on client side. • For example, an image can be changed to another when a user moves his mouse over the image.
How to use JavaScript • JavaScript code can be inserted into a HTML file. • In both <head> and <body> • For example <head> <title>JS Hello World</title> <script type="text/javascript"> window.alert("Hello World"); </script> </head>
How to use JavaScript • You can insert JavaScript in <body> as well • For example <body> <script type="text/javascript"> window.alert("Hello World"); </script> </body>
How to use JavaScript • JavaScript code in HTML must be inside the <script> tag. • JavaScript can be also included from external sources using <script> tag. • Just put the following into <head> tag • <script type="text/javascript” src=“filename.js>
How to use JavaScript • JavaScript code in HTML must be inside the <script> tag. • JavaScript can be also included from external sources using <script> tag. • Just put the following into <head> tag • <script type="text/javascript” src=“filename.js>
JavaScript Syntax • Every JavaScript statement must end with a semicolon ; • If it is inside a HTML file, JavaScript must be inside the <script> tag • For example, <script> var number = 10; //declare a variable named number <script>
JavaScript Syntax • JavaScript’s basic syntax is identical to that of C++ and Java. • // begins a comment that ends at the end of the line • /* and */ may be used to contain multiline comments • For instance //This is number 1 /*I assigned 10 to the variable number.*/
JavaScript Syntax • Variables names are case sensitive. • JavaScript does not have typed variables. • The variable declarations do not specify a data type for the variables • When you declare a variable, always start with keyword var var num1 = 10; var Num1 = 20; //this is ok because of case sensitivity var color = ‘c’; varfirstname = “David”
JavaScript Syntax • Similarities between JavaScript and C++. • Most of operators are the same as in Java and C++ • !=, +, &, /, %, >, < • JavaScript uses braces to enclose statement blocks and its syntax for assignment (=) and control statements (if, while) are the same as in Java and C++.
Next class • Variables and Data Types • Statements • Expression • Block statement • Keyword statement • Operators • +, -, *, /, %, ==, ++, --, <, >, etc