JavaScript syntax

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

In programming languages, variables are used to store data values. A variable is a name. A literal is a value.

JavaScript uses the keyword var to define variables and the equal sign to assign values ​​to variables:

<!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


JavaScript uses assignment operators to variables Assignment:


<!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


The JavaScript language has many types of operators:

TypeInstanceDescriptionAssignment, arithmetic and bitwise operators= + - * /Description in JS operatorsConditional, comparison and logical operators== != < > Comparison in JS Operators are described in

JavaScript Statement

In HTML, a JavaScript statement is a command issued to the browser.

Statements are separated by semicolons:

x = 5 + 6;
y = x * 10;

JavaScript keywords

JavaScript statements usually begin with keywords. The var keyword tells the browser to create a new variable:

var x = 5 + 6;
var y = x * 10;

JavaScript Keywords

Like any other programming language, JavaScript reserves some keywords for its own use.

JavaScript also reserves some keywords that are not used in the current language version, but will be used in future JavaScript extensions.

JavaScript keywords must begin with a letter, an underscore (_), or a dollar sign ($).

Following characters can be letters, numbers, underscores or dollar signs (numbers are not allowed to appear as the first character, so that JavaScript can easily distinguish keywords and numbers).

The following are the most important reserved words in JavaScript (in alphabetical order):

abstractelseinstanceofsuper




booleanenumintswitch




breakexportinterfacesynchronized




byteextendsletthis




casefalselongthrow




catchfinalnativethrows




charfinallynewtransient




classfloatnulltrue




constforpackagetry




continuefunctionprivatetypeof




debuggergotoprotectedvar




defaultifpublicvoid




deleteimplementsreturnvolatile




doimportshortwhile




doubleinstaticwith





JavaScript 对大小写敏感。

JavaScript 对大小写是敏感的。

当编写 JavaScript 语句时,请留意是否关闭大小写切换键。

函数 getElementById 与 getElementbyID 是不同的。

同样,变量 myVariable 与 MyVariable 也是不同的。



Continuing Learning
||
<!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>
submitReset Code