avaScript is a programming language. Grammar rules define the structure of a language.
JavaScript Syntax
JavaScript is a scripting language.
It is a lightweight, yet powerful programming language.
JavaScript Literal
In a programming language, a literal is a constant, as in 3.14.
Number literal can be an integer or a decimal, or scientific notation (e).
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 123e5; </script> </body> </html>
Run the program and try it
String literal You can use single quotes or double quotes:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo">andy</p> <script> document.getElementById("demo").innerHTML = 'liu qi'; </script> </body> </html>
Run the program and try it
Expression literals are used for calculations:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 * 10; </script> </body> </html>
Run the program and try it
Array (Array) literals define an array:
[40, 100, 1, 5, 25, 10]
Object literal defines an object:
{firstName: "John", lastName:"Doe", age:50, eyeColor:"blue"}
Function (Function) literal defines a function:
function myFunction(a, b ) { return a * b;}
#JavaScript Variable
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var length; length = 6; document.getElementById("demo").innerHTML = length; </script> </body> </html>Run the program and try it
JavaScript Operator
JavaScript uses arithmetic operators to calculate values:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = (5 + 6) * 10; </script> </body> </html>Run the program to try it
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p id="demo"></p> <script> var x, y, z; x = 5 y = 6; z = (x + y) * 10; document.getElementById("demo").innerHTML = z; </script> </body> </html>Run the program and try it
Instance | Description | ||
---|---|---|---|
= + - * / | Description in JS operators | ||
== != < > | Comparison in JS Operators are described in |
abstract | else | instanceof | super |
boolean | enum | int | switch |
break | export | interface | synchronized |
byte | extends | let | this |
case | false | long | throw |
catch | final | native | throws |
char | finally | new | transient |
class | float | null | true |
const | for | package | try |
continue | function | private | typeof |
debugger | goto | protected | var |
default | if | public | void |
delete | implements | return | volatile |
do | import | short | while |
double | in | static | with |
JavaScript 对大小写敏感。
JavaScript 对大小写是敏感的。
当编写 JavaScript 语句时,请留意是否关闭大小写切换键。
函数 getElementById 与 getElementbyID 是不同的。
同样,变量 myVariable 与 MyVariable 也是不同的。