JavaScript Syntax


JavaScript is a programming language. Syntax rules define the language structure.


JavaScript Syntax

JavaScript is a scripting language.

It is a lightweight but powerful programming language.


JavaScript Literals

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

Number LiteralsThey can be integers or decimals, or scientific notation (e).

3.14

1001

123e5

Try it yourself »

String LiteralsYou can use single or double quotes:

"John Doe"

'John Doe'

Try it yourself »

Expression LiteralsUsed for calculation:

5 + 6

5 * 10

Try it yourself »

Array LiteralsDefine an array:

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

Object LiteralsDefine an object:

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

Function LiteralsDefine a function:

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


JavaScript Variables

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

JavaScript uses keywordsvarto define variables, and use the equals sign to assign values to variables:

var x, length

x = 5

length = 6

Try it yourself »

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

Note A variable is aname.A literal is aValue。

JavaScript Operators

JavaScript usesArithmetic Operatorsto calculate values:

(5 + 6) * 10

Try it yourself »

JavaScript usesAssignment Operatorsto assign values to variables:

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

Try it yourself »

The JavaScript language has many types of operators:

Type Examples Description
Assignment, Arithmetic, and Bitwise Operators =  +  -  *  / Described in JS Operators
Conditional, Comparison, and Logical Operators ==  != <  >  Described in JS Comparison Operators


JavaScript Statements

In HTML, JavaScript statements are used to send commands to the browser.

Statements are separated by semicolons:

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


JavaScript Keywords

JavaScript keywords are used to identify operations to be performed.

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

varThe keyword tells the browser to create a new variable:

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

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

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

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 Comments

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

// I will not execute

JavaScript Data Types

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

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


Concept of Data Types

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

In order to operate on variables, it is very important to understand the concept of data types.

Without data types, the following example cannot be executed:

16 + "Volvo"

How is 16 plus "Volvo" calculated? Will the above produce an error or output the following result?

"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 Functions

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

Referencing a function= calling the function (executing the statements inside the function).

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


JavaScript Case Sensitivity

JavaScript is case-sensitive.

When writing JavaScript statements, please be mindful of whether the caps lock key is turned off.

FunctionsgetElementByIdandgetElementbyIDare different.

Similarly, variablesmyVariableandMyVariableare also different.


JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers all characters, including punctuation and other characters.

For more information, please study ourComplete Unicode Reference Manual。


Did you know?

Note
In JavaScript, camel case naming convention is common, such as lastName (rather than lastname).
Other extensions