X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

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 (Fixed Values)

In computer science , literals are representing fixed values.

With JavaScript, the most important rules for writing fixed (constant) values in are:

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

3.14

1001

123e5

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 »


JavaScript Variables

In a programming language (and in algebra), variables are used to store 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, length 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. It 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 »


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 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 Identifiers

In a programming language, all variables must be identified 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 is Case Sensitive

JavaScript identifiers are case sensitive

The variables lastName and lastname, are two different variables.

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?

Note
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:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook