JavaScript
• JavaScript code is included within <script> tags
Sample code:
<script >
document.write("<h1>Hello World!</h1>") ;
</script>
· This JavaScript will be executed when the form object is used
Primitive Data Types
• JavaScript has three “primitive” types:
Ø Number
Ø Numbers are always stored as floating-point values
• Hexadecimal numbers begin with 0x.
• Some platforms treat 0123 as octal, others treat it as decimal.
Ø String
• Strings may be enclosed in single quotes or double quotes.
• Strings can contains \n (newline), \" (double quote), etc.
Ø Boolean
· Booleans are either true or false.
• 0, "0", empty strings, undefined, null, and NaN are false , other values are true.
Variables
Most JavaScript syntax is borrowed from C .
• Arithmetic operators:
+ - * / % ++ --
• Comparison operators:
< : Returns true if the variable on the left is less than the variable on the right eg: X<Y.
<= : Returns true if the variable on the left is less than or equal to right. Eg: X<=Y
= = : Returns true if variables are equal. Eg: X=Y
!= : Returns true if variables are not equal.Eg:X!= Y
>= : Returns true if the variable on the left is greater than or equal to the variable on the right.eg X>=Y.
> : : Returns true if the variable on the left is greater than the variable on the right eg: X>Y.
• Logical operators:
&& || ! (&& and || are short-circuit operators).
The logical operator && returns a value of true only if all of the Boolean expressions are true.
• Bitwise operators:
& | ^ ~ << >> >>>
• Assignment operators:
+= -= *= /= %= <<= >>= >>>= &= ^= |=
Expressions assign values using assignment operators. “=” is the most common one.
• String operator:
+
• The conditional operator:
Tests whether a specific condition is true and returns one value if the condition is true and a different value if the condition is false.
condition ? value_if_true : value_if_false
• Special equality tests:
• == and != try to convert their operands to the same type before performing the test
• === and !== consider their operands unequal if they are of different types
• Additional operators (to be discussed):
new typeof void delete
Comments
• Comments are as in C or Java:
– Between // and the end of the line
– Between /* and */
• Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments; they have no special meaning in JavaScript
Creating JavaScript Functions
• Function names are case-sensitive.
• The function name must begin with a letter or underscore ( _ ) and cannot contain any spaces.
• There is no limit to the number of function parameters that a function may contain.
• The parameters must be placed within parentheses, following the function name, and the parameters must be separated by commas.
Exception handling
• Exception handling in JavaScript is almost the same as in Java
• throw expression creates and throws an exception
– The expression is the value of the exception, and can be of any type (often, it's a literal String)
• try {
statements to try
} catch (e) { // Notice: no type declaration for e
exception-handling statements
} finally { // optional, as usual
code that is always executed
}
– With this form, there is only one catch clause
• try {
statements to try
} catch (e if test1) {
exception-handling for the case that test1 is true
} catch (e if test2) {
exception-handling for when test1 is false and test2 is true
} catch (e) {
exception-handling for when both test1and test2 are false
} finally { // optional, as usual
code that is always executed
}
• Typically, the test would be something like
e == "InvalidNameException"
JavaScript Syntax Issues
• JavaScript commands and names are case-sensitive.
• JavaScript command lines end with a semicolon to separate it from the next command line in the program.
• In some situations, the semicolon is optional.
• semicolons are useful to make your code easier to follow and interpret.
- JavaScript is an interpreted programming or script language from Netscape.
- JavaScript is used in Web site development .
- JavaScript is reasonably platform-independent.
- Statements in JavaScript resemble statements in Java, because both languages borrowed heavily from the C language.
- JavaScript is often used in conjunction with HTML forms.
- JavaScript is a complete, full-featured, complex language.
- It can automatically change a formatted date on a Web page.
- It is used to linked-to-page to appear in a popup window.
- It is used in text or a graphic image to change during a mouse rollover.
- Internet Explorer supports JScript.
- JScript is identical to JavaScript, but there are some JavaScript commands not supported in JScript, and vice versa.
- Other client-side programming languages are also available to Web page designers, such as the Internet Explorer scripting language, VBScript.
• JavaScript code is included within <script> tags
Sample code:
<script >
document.write("<h1>Hello World!</h1>") ;
</script>
- The type attribute is to allow to use other scripting languages.
- JavaScript functions should be defined in the <head>
- This ensures that the function is loaded before it is needed
- JavaScript in the <body> will be executed as the page loads
- <script src="myJavaScriptFile.js"></script>
- Put this HTML wherever you would put the actual JavaScript code
- An external .js file lets you use the same JavaScript on multiple HTML pages
- The external .js file cannot itself contain a <script> tag
· This JavaScript will be executed when the form object is used
Primitive Data Types
• JavaScript has three “primitive” types:
Ø Number
Ø Numbers are always stored as floating-point values
• Hexadecimal numbers begin with 0x.
• Some platforms treat 0123 as octal, others treat it as decimal.
Ø String
• Strings may be enclosed in single quotes or double quotes.
• Strings can contains \n (newline), \" (double quote), etc.
Ø Boolean
· Booleans are either true or false.
• 0, "0", empty strings, undefined, null, and NaN are false , other values are true.
Variables
- Variables are declared with a var statement.
- Variables names must begin with a letter or underscore.
- Variable names are case-sensitive .
- Variables are untyped .
- Variables declared within a function are local to that function .
- Variables declared outside a function are global .
Most JavaScript syntax is borrowed from C .
• Arithmetic operators:
+ - * / % ++ --
• Comparison operators:
< : Returns true if the variable on the left is less than the variable on the right eg: X<Y.
<= : Returns true if the variable on the left is less than or equal to right. Eg: X<=Y
= = : Returns true if variables are equal. Eg: X=Y
!= : Returns true if variables are not equal.Eg:X!= Y
>= : Returns true if the variable on the left is greater than or equal to the variable on the right.eg X>=Y.
> : : Returns true if the variable on the left is greater than the variable on the right eg: X>Y.
• Logical operators:
&& || ! (&& and || are short-circuit operators).
The logical operator && returns a value of true only if all of the Boolean expressions are true.
• Bitwise operators:
& | ^ ~ << >> >>>
• Assignment operators:
+= -= *= /= %= <<= >>= >>>= &= ^= |=
Expressions assign values using assignment operators. “=” is the most common one.
• String operator:
+
• The conditional operator:
Tests whether a specific condition is true and returns one value if the condition is true and a different value if the condition is false.
condition ? value_if_true : value_if_false
• Special equality tests:
• == and != try to convert their operands to the same type before performing the test
• === and !== consider their operands unequal if they are of different types
• Additional operators (to be discussed):
new typeof void delete
Comments
• Comments are as in C or Java:
– Between // and the end of the line
– Between /* and */
• Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments; they have no special meaning in JavaScript
Creating JavaScript Functions
• Function names are case-sensitive.
• The function name must begin with a letter or underscore ( _ ) and cannot contain any spaces.
• There is no limit to the number of function parameters that a function may contain.
• The parameters must be placed within parentheses, following the function name, and the parameters must be separated by commas.
Exception handling
• Exception handling in JavaScript is almost the same as in Java
• throw expression creates and throws an exception
– The expression is the value of the exception, and can be of any type (often, it's a literal String)
• try {
statements to try
} catch (e) { // Notice: no type declaration for e
exception-handling statements
} finally { // optional, as usual
code that is always executed
}
– With this form, there is only one catch clause
• try {
statements to try
} catch (e if test1) {
exception-handling for the case that test1 is true
} catch (e if test2) {
exception-handling for when test1 is false and test2 is true
} catch (e) {
exception-handling for when both test1and test2 are false
} finally { // optional, as usual
code that is always executed
}
• Typically, the test would be something like
e == "InvalidNameException"
JavaScript Syntax Issues
• JavaScript commands and names are case-sensitive.
• JavaScript command lines end with a semicolon to separate it from the next command line in the program.
• In some situations, the semicolon is optional.
• semicolons are useful to make your code easier to follow and interpret.