JavaScript Syntax

JavaScript is a programming language. The Syntax rules define how the language is constructed.

JavaScript Syntax

JavaScript is a scripting language. It is a lightweight, but powerful, programming language.

Syntax definition: "The principles by which sentences are constructed in a language."

The sentences of a programming language are called computer statements, or just statements.

JavaScript Literals

In a programming language, a literal is a constant value, like 3.14.

Number literals can be written with or without decimals, and with or without scientific notation (e):




Try it Yourself »

String literals can be written with double or single quotes:

"John Doe"

'John Doe'

Try it Yourself »

Expression literals evaluates (computes) to a value:

5 + 6

5 * 10

Try it Yourself »

Array literals defines an array:

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

Object literals defines an object:

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

Function literals defines a function:

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

JavaScript Variables

In a programming language (and in normal algebra), named variables store data values.

JavaScript uses the var keyword to define variables, and an equal sign to assign values to variables (just like algebra):

var x, length

x = 5

length = 6

Try it Yourself »

A variable can have variable values during the execution of a JavaScript. A literal is always a constant value.

Note A variable is a name. A literal is value.

JavaScript Operators

JavaScript uses arithmetic operators to compute values (just like algebra):

(5 + 6) * 10

Try it Yourself »

JavaScript uses an assignment operator to assign values to variables (just like algebra):

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 Comparisons

JavaScript Statements

In HTML, JavaScript statements are written as sequences of "commands" to the HTML browser.

Statements are separated by semicolons:

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

JavaScript Keywords

A JavaScript statement often starts with a keyword. The var keyword tells the browser to create a new variable:

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

JavaScript Identifiers

All programming languages must identify variables, functions, and objects, with unique names.

These unique names are called identifiers.

Identifier names can contain letters, digits, underscores, and dollar signs, but cannot begin with a number.

Reserved words (like JavaScript keywords) cannot be used as identifiers.

JavaScript Comments

Not all JavaScript statements are "commands". Anything after double slashes // is ignored by the browser:

// I will not be executed

JavaScript Data Types

JavaScript variables can hold many types of data: numbers, text strings, arrays, objects and much more:

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

Note We use blue color to highlight reserved words, brown for string literals, and green for comments.

JavaScript Functions

JavaScript statements written inside a function, can be invoked many times (reused):

Invoke a function = Call upon a function (ask for the code in the function to be executed).

function myFunction(a, b) {
    return a * b;                              // returns the product of a and b

JavaScript is Case Sensitive

In JavaScript all identifiers are case sensitive. 

The variables lastName and lastname, are two different variables.

The functions myFunction and myfunction, are two different functions.

JavaScript does not interpret Var; as var.

JavaScript Character Set

JavaScript uses the Unicode character set.

Unicode covers (almost) all the characters, punctuations, and symbols in the world.

For a closer look, please study our Complete Unicode Reference.

Did You Know?

It is common, in JavaScript, to use camelCase names.
You will often see identifier names written like lastName (instead of lastname).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3c-schools.com: