JavaScript Syntax

JavaScript GrammarIt is a set of rules that defines the structure of the JavaScript language.

var x, y; // How to declare variables
x = 7; y = 8; // How to assign values
z = x + y; // How to calculate a value

JavaScript values

JavaScript defines two types of values through statements: mixed values and variable values.

Mixed values are calledLiterals (literal). Variable values are calledvariables.

JavaScript literals

The most important rule for writing mixed values is:

WriteNumberWhether there is a decimal point or not:

15.90
10011

Try it yourself

StringIs text enclosed by double or single quotes:

"Bill Gates"
'Bill Gates' 

Try it yourself

JavaScript Variables

In programming languages,variablesused forstoredata values.

Using JavaScript var keywords todeclarationvariable.

= number is used toassignment.

In this example, x is defined as a variable. Then, the value assigned to x is 7:

var x;
x = 7;

Try it yourself

JavaScript Operators

Using JavaScriptarithmetic operators(+ - * /) tocalculate value:

(7 + 8) * 10

Try it yourself

Using JavaScriptassignment operator(=)to variablesassignment:

var x, y;
var x = 7;
var y = 8;

Try it yourself

JavaScript Expressions

Expressions are combinations of values, variables, and operators, with a calculated result being a value.

6 * 10

Try it yourself

Expressions can also include variable values:

x * 10

Try it yourself

Values can be of various types, such as numbers and strings.

For example, "Bill" + " " + "Gates" is calculated as "Bill Gates":

"Bill" + " " + "Gates"

Try it yourself

JavaScript Keywords

JavaScript Keywordsused to identify actions to be executed.

var Keywords inform the browser to create a new variable:

var x = 7 + 8;
var y = x * 10; 

Try it yourself

JavaScript Comments

Not all JavaScript statements are "executed".

double slashes // or /* and */ The code betweenComments.

Comments are ignored and will not be executed:

var x = 7;   // will be executed
// var x = 8;   will not be executed

Try it yourself

JavaScript Identifiers

Identifiers are names.

In JavaScript, identifiers are used to name variables (as well as keywords, functions, and labels).

In most programming languages, the rules for valid names are mostly the same.

In JavaScript, the first character must be a letter, underscore (-), or dollar sign ($).

Sequential characters can be letters, numbers, underscores, or dollar signs.

Tip:Numbers cannot be the first character. This allows JavaScript to easily distinguish identifiers from numbers.

JavaScript is case-sensitive

All JavaScript identifiersare case-sensitive.

variables lastName and lastnameas two different variables.

lastName = "Gates";
lastname = "Jobs"; 

Try it yourself

JavaScript does not treat VAR or Var translated as keyword var.

JavaScript and CamelCase Naming

Historically, programmers have used three methods to connect multiple words into a variable name:

Hyphen:

first-name, last-name, master-card, inter-city.

Comments:Hyphens cannot be used in JavaScript. It is reserved for subtraction.

Underscore:

first_name, last_name, master_card, inter_city.

Camel Case:

FirstName, LastName, MasterCard, InterCity.
camelCase

JavaScript programmers tend to use camel case with lowercase letters as the beginning:

firstName, lastName, masterCard, interCity

JavaScript Character Set

Using JavaScript Unicode Character Set.

Unicode covers almost all characters, punctuation, and symbols in the world.

For more details, please visit our complete Unicode Reference Manual.