JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Syntax

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program.


JavaScript Variables

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

Variables in standard JavaScript have no type attached, and any value can be stored in any variable.

Variables can be declared with let (for a block level variable), var (for a function level variable) or const (for an immutable).

An equal sign is used to assign values to variables.

In this example, num is defined as a variable. Then, num is assigned the value 20:

Run code

JavaScript Operators

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

Run code

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

Run code

You will learn more about operators later in this tutorial.


JavaScript Identifiers

The name of a variable, function, or property is known as an identifier in JavaScript.

The rules for legal names are much the same in most programming languages.

Identifiers consist of letters and numbers, but they cannot include any symbol outside of $ and _, and cannot begin with a number.

Numbers are not allowed as the first character. This way JavaScript can easily distinguish identifiers from numbers.


JavaScript Comments

A comment is simply a line of text that is completely ignored by the JavaScript interpreter.

Comments are usually added with the purpose of providing extra information pertaining to source code.

JavaScript support single-line as well as multi-line comments.

Single-line comments begin with a double forward slash (//), followed by the comment text.

Run code

Whereas, a multi-line comment begins with a slash and an asterisk (/*) and ends with an asterisk and slash (*/).

Run code

You will learn more about comments later in this tutorial.



JavaScript is Case Sensitive

All JavaScript identifiers are case-sensitive.

The variables myVariable and myvariable, are two different variables:

Run code

JavaScript and Camel Case

The convention of JavaScript identifiers is that they are written in camelCase, meaning the first word is lowercase but every following word starts with an uppercase letter.

Run code

You may also see global variables or constants written in all uppercase.

Run code

JavaScript Indentation

A complete JavaScript program can technically be written on a single line.

However, this would quickly become very difficult to read and maintain. Instead, we use newlines and indentation.

Here's an example of a conditional if/else statement, written on either one line or with newlines and indentation.

Notice that any code included within a block is indented. The indentation can be done with two spaces, four spaces, or by pressing the tab character.