JavaScript Syntax

JavaScript is a programming language. The grammar rules define the structure of the language.

JavaScript Syntax

JavaScript is a scripting language.

It is a lightweight but powerful programming language.

JavaScript Literal

In programming languages, generally fixed values are called literals, such as 3.14.

Number (Number) literal can be an integer or decimal, or scientific notation (e).




 Try It! 

String literals can use single or double quotes:

"John Doe"

'John Doe'

 Try It! 

Expression literals are used in calculations:

5 + 6

5 * 10

 Try It! 

Array literal Define an array:

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

Object literal defines 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:

var x, length
x = 5
length = 6

 Try It! 

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

NoteThe variable is a name . The literal is a value .

JavaScript Operators

JavaScript uses arithmetic operators calculated value:

(5 + 6) * 10

 Try It! 

JavaScript uses assignment operators to assign values to variables:

x = 5
y = 6
z = (x + y) * 10

Try It! 

There are many types of operators in the JavaScript language:

Assignment, arithmetic and bit operators= + - * /Described in JS operators
Conditions, comparisons and logical operators== != <> Described in JS comparison operators

JavaScript Statement

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

The statements are separated by semicolons:

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

JavaScript Keywords

JavaScript keywords are used to identify the action to be performed.

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

The var keyword tells the browser to create a new variable:

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

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

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


JavaScript Comments

Not all JavaScript statements are "commands". The content after the double slash // will be ignored by the browser:

// I will not execute

JavaScript Data Types

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

var length = 16; // Number is assigned by number literal
var points = x * 10; // Number is assigned by expression literal
var lastName = "Johnson"; // String is assigned by string literal
var cars = [" Saab", "Volvo", "BMW"]; // Array is assigned by array literal
var person = {firstName:"John", lastName:"Doe"}; // Object is assigned by object literal

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 important to understand the concept of data types.

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

16 + "Volvo"

16 How to add "Volvo" to calculate? Will the above produce an error or output the following result?


You can try to execute 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 in functions, and functions can be quoted repeatedly:

Reference a function = call function (execute the statement within the function).

function myFunction(a, b) {
    return a * b; // return the result of a multiplied by b

JavaScript Letter Case

JavaScript is case sensitive.

When writing JavaScript statements, please pay attention to whether to turn off the case switch key.

The function getElementById is different from getElementbyID .

Similarly, the variables myVariable and MyVariable are also different.

JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers all characters, including characters such as punctuation.

For more information, please study our full Unicode reference manual.

Do you know?

In JavaScript, the common camel case naming rule, such as lastName (not lastname).