JavaScript Syntax

JavaScript Syntax

JavaScript is a scripting language. A scripting language is a lightweight programming language.

The sentences in a programming language are called statements.

The principles, how sentences are constructed in a language, are called language syntax.

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 evaluate (compute) to values:

5 + 6

5 * 10

Try it Yourself »

Array literals define arrays:

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

Object literals define objects:

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

JavaScript Variables

In a programming language (and in algebra), variables are used to store data values.

JavaScript uses the var keyword to define variables.

An equal sign is used to assign values to variables (just like algebra).

In this example, length is defined as a variable. Then, it is assigned (given) the value 6:

var length;

length = 6;

Try it Yourself »

Note A literal is a fixed value. A variable is a name that can have variable values.

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

A computer program is a sequences of "executable commands" called statements.

JavaScript statements are separated by semicolon:

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

When separated by semicolon, multiple statements on one line is allowed:

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 all variables with unique names.

These unique names are called identifiers.

Identifiers 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 "executable commands".

Anything after double slashes // is treated as a comment.

Comments are ignored, and will not be executed:

// x = 5 + 6; I will not be executed

Note In this tutorial, we use colors to highlight reserved words, values, and comments.

JavaScript Data Types

JavaScript variables can hold many data types: numbers, strings, arrays, objects and 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 x = {firstName:"John", lastName:"Doe"};    // Object assigned by an object literal

The Concept of Data Types

In programming, data types is an important concept.

To be able to operate on variables, it is important to know something about the type.

Without data types, a computer can not safely solve this:

16 + "Volvo"

Does it make any sense to add "Volvo" to sixteen? Will it produce an error or a result?


You will learn much more about data types in a later chapter.

JavaScript Functions

JavaScript statements written inside a function, can be invoked and reused many times.

Invoke a function = call a function (ask for the statements in the function to be executed).

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

JavaScript is Case Sensitive

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

Try it Yourself Summary

JavaScript Numbers

JavaScript Strings

JavaScript Expressions

JavaScript Variables

JavaScript Operators

JavaScript Assignment

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:


UK Reseller Hosting


Download XML Editor FREE Website BUILDER Free HTML5 Templates