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

JavaScript Syntax


JavaScript Syntax

The rules, how a language is constructed, are called language syntax.

The sentences in a programming language are called program statements.


JavaScript Statements

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

JavaScript statements are separated by semicolon.

greeting = "Hello";
document.getElementById("demo").innerHTML = greeting;

Try it Yourself »

JavaScript statements are composed of:

Values, Variables, Operators, Expressions, Keywords, and Comments.


JavaScript (Fixed) Values

With JavaScript, the most important rules for writing values are:

Numbers are written with or without decimals, and with or without scientific notation (e):

10.50

1001

123e5

Try it Yourself »

Strings are written with double or single quotes:

"John Doe"

'John Doe'

Try it Yourself »

An expressions is also a value:

5 + 6

5 * 10

Try it Yourself »

Note In computer science, fixed values are called literals.


JavaScript Variables

In a programming language, variables are used to store values.

JavaScript uses the var keyword to define variables.

An equal sign is used to assign values to variables.

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

var x;

x = 6;

Try it Yourself »


JavaScript Operators

JavaScript uses an assignment operator ( = ) to assign values to variables:

var x = 5;
var y = 6;

Try it Yourself »

JavaScript uses arithmetic operators ( + - *  / ) to compute values:

(5 + 6) * 10

Try it Yourself »


JavaScript Keywords

A JavaScript statement often starts with a keyword to identify an action to be performed.

The var keyword tells the browser to create a new variable:

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

Try it Yourself »


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:

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

Try it Yourself »


JavaScript is Case Sensitive

All JavaScript identifiers are case sensitive

The variables lastName and lastname, are two different variables.

JavaScript does not interpret VAR or Var as the keyword var.

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


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.



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