Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript syntax

JavaScript syntax


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


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 literals They can be integers or decimals, or written in scientific notation (e).

3.14

1001

123e5

Try it »

String literals Can use single or double quotes:

"John Doe"

'John Doe'

Try it »

Expression literals Used for computation:

5 + 6

5 * 10

Try it »

Array literals Define an array:

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

Object literals Define an object:

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

Function literals 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 a variable, and use the equals sign to assign a value to it:

var x, length

x = 5

length = 6

Try it »

Variables can be accessed by their variable names. 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 uses Arithmetic operators to compute values:

(5 + 6) * 10

Try it »

JavaScript usesAssignment operatorsAssign a value to a variable:

x = 5

y = 6

z = (x + y) * 10

Try it »

The JavaScript language has several types of operators:

Type Example 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 issue commands to the browser.

Statements are separated by semicolons:

x = 5 + 6;

y = x * 10;


JavaScript keywords

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

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

var A 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 may 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'. A double slash // followed by content that the browser ignores:

// I will not execute


JavaScript Data Types

JavaScript has several data types: numbers, strings, arrays, objects, and so on:

var length = 16; // Number assigned via a numeric literal

var points = x * 10; // Number assigned via an expression literal

var lastName = "Johnson"; // String assigned via a string literal

var cars = ["Saab", "Volvo", "BMW"]; // Array assigned via an array literal

var person = {firstName:"John", lastName:"Doe"}; // Object assigned via an object literal


The concept of data types

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

To work with variables, it is important to understand the concept of data types.

Without data types, the following example could not be executed:

16 + "Volvo"

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

"16Volvo"

You can try running the above code in the browser to see the effect.

You will learn more about data types in the following chapters.


JavaScript Functions

JavaScript statements can be written inside functions, which can be reused:

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 letter case

JavaScript is case-sensitive.

When writing JavaScript statements, be careful about the Caps Lock key.

function getElementById The difference between getElementbyID are different.

Likewise, the variables myVariable The difference between MyVariable are also different.


JavaScript character set

JavaScript uses the Unicode character set.

Unicode covers all characters, including punctuation and other symbols.

To learn more, please study our Complete Unicode Reference Manual。


Did you know?

Note In JavaScript, the camelCase naming convention is common, such as lastName (rather than lastname).