JavaScript syntax



JavaScript 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).

Instance

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

Click the "Run Instance" button to view the online instance

String literal You can use single or double quotes:

Example

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
</head>
<body>
	
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 'John Doe';
</script>
	
</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

Expression literal is used for calculation:

Example

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

Click the "Run instance" button to view the online instance

##Array ) Literal Define an array:

[40, 100, 1, 5, 25, 10]

Object literal Define an object:

{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}

Function literal Define a function:

function myFunction(a, b) { return a * b;}

JavaScript Variables

In programming languages, variables are used to store data values.

JavaScript uses the keyword

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

Example

<!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 Instance»Click the "Run Instance" button to view the online instance

Variables can be accessed by variable name. In imperative languages, variables are usually mutable. A literal is a constant value.

The variable is a

JavaScript Operators

JavaScript uses arithmetic operators to calculate values:

Example

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

Click the "Run Example" button to view the online example

JavaScript uses the assignment operatorto assign a value to a variable:

Instance

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

Click the "Run Instance" button to view the online instance

JavaScript language has many types of operators:

Notename. A literal is a value.
TypeInstanceDescription
Assignment, arithmetic and bitwise operators= + - * /Description in JS operators
Conditions, comparisons and Logical operators== != < > Described in JS comparison operators



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 start 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):

##booleanenumintswitch#break##byteextendsletthisfalsefinalnativethrows##charnewnulltruefortry##continuetypeofprotectedif#deletevolatileshortin##

JavaScript Notes

Not all JavaScript statements are "commands". Content after double slashes // will be ignored by the browser:

// I will not execute

JavaScript data Type

JavaScript has many data types: numbers, strings, arrays, objects, etc.:

var length = 16; var points=x "BMW"]; // Array Assignment through array literals
var person = {firstName:"John", lastName:"Doe"}; // Object Assignment through object literals



The concept of data types


In programming languages, data types are a very important content.

In order to be able to manipulate variables, it is very important to understand the concept of data types.

If the data type is not used, the following example will not be executed:

16 + "Volvo"

16 How to calculate "Volvo" What? Will the above generate an error or output the following results?

"16Volvo"

You can try executing the above code in your browser to see the effect.
In the following chapters you will learn more about data types.

JavaScript function


JavaScript statements can be written within functions, and functions can be referenced repeatedly:

Referencing a function

= Calling a function (executing statements within functions).

function myFunction(a, b) {

return a * b;                                       // Return the result of a multiplied by b
}



JavaScript is case-sensitive.

JavaScript is case-sensitive.

When writing JavaScript statements, please pay attention to whether the case switching key is turned off.

Function getElementById is different from getElementbyID.

Similarly, variables myVariable and MyVariable are also different.


JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers all characters, including punctuation and other characters.

To learn more, study our complete Unicode reference manual.


Did you know?

abstractelse instanceofsuper








exportinterfacesynchronized








#case
longthrow



#catch




finally
transient



##class
float


#const

package



function
private


#debugger
goto

var

#default

public
void


implements
return


#do
import

while

# double

static
with

Note
In JavaScript, the common naming method is camel case Rules such as lastName (not lastname).