ListMoto - JavaScript Syntax

--- Advertisement ---

See also

* ECMAScript

* v * t * e

The SYNTAX OF JAVASCRIPT is the set of rules that define a correctly structured JavaScript

The examples below make use of the log function of the console object present in most browsers for standard text output .

The JavaScript
standard library lacks an official standard text output function. Given that JavaScript
is mainly used for client-side scripting within modern Web browsers , and that almost all Web browsers provide the alert function, alert can also be used, but is not commonly used.


* 1 Origins

* 2 Basics

* 2.1 Case sensitivity * 2.2 Whitespace and semicolons * 2.3 Comments

* 3 Variables

* 3.1 Scoping and hoisting * 3.2 Declaration and assignment * 3.3 Examples

* 4 Primitive data types

* 4.1 Undefined * 4.2 Null * 4.3 Number * 4.4 String * 4.5 Boolean * 4.6 Symbol

* 5 Native objects

* 5.1 Array * 5.2 Date * 5.3 Error * 5.4 Math

* 5.5 Regular expression
Regular expression

* 5.5.1 Character classes * 5.5.2 Character matching * 5.5.3 Repeaters * 5.5.4 Anchors * 5.5.5 Subexpression * 5.5.6 Flags * 5.5.7 Advanced methods * 5.5.8 Capturing groups

* 5.6 Function

* 6 Operators

* 6.1 Arithmetic

* 6.2 Assignment

* 6.2.1 Destructuring assignment * 6.2.2 Spread/rest operator

* 6.3 Comparison * 6.4 Logical * 6.5 Bitwise * 6.6 Bitwise Assignment * 6.7 String

* 7 Control structures

* 7.1 Compound statements * 7.2 If ... else * 7.3 Conditional operator * 7.4 Switch statement * 7.5 For loop * 7.6 For ... in loop * 7.7 While loop * 7.8 Do ... while loop * 7.9 With * 7.10 Labels

* 8 Functions

* 9 Objects

* 9.1 Creating objects * 9.2 Methods * 9.3 Constructors * 9.4 Inheritance

* 10 Exception handling

* 11 Native functions and methods

* 11.1 eval (expression)

* 12 See also * 13 References * 14 Further reading * 15 External links


Brendan Eich
Brendan Eich
summarized the ancestry of the syntax in the first paragraph of the JavaScript
1.1 specification as follows:

borrows most of its syntax from Java , but also inherits from Awk and Perl
, with some indirect influence from Self in its object prototype system.



is case sensitive. It is common to start the name of a constructor with a capitalised letter, and the name of a function or variable with a lower-case letter.


var a=5; console.log(a); // 5 console.log(A); // throws a ReferenceError: A is not defined


Spaces , tabs and newlines used outside of string constants are called whitespace . Unlike C , whitespace in JavaScript
source can directly impact semantics. Because of a technique called "automatic semicolon insertion " (ASI), some statements that are well formed when a newline is parsed will be considered complete (as if a semicolon were inserted just prior to the newline). Some authorities advise supplying statement-terminating semicolons explicitly, because it may lessen unintended effects of the automatic semicolon insertion.

There are two issues: five tokens can either begin a statement or be the extension of a complete statement; and five restricted productions, where line breaks are not allowed in certain positions, potentially yielding incorrect parsing.

The five problematic tokens are the open parenthesis "(", open bracket "

a = b + c (d + e).foo() // Treated as: // a = b + c(d + e).foo();

with the suggestion that the preceding statement be terminated with a semicolon.

Some suggest instead the use of leading semicolons on lines starting with '(' or ' For example:

a = b + c ;(d + e).foo() // Treated as: // a = b + c; // (d + e).foo();

Initial semicolons are also sometimes used at the start of JavaScript libraries, in case they are appended to another library that omits a trailing semicolon, as this can result in ambiguity of the initial statement.

The five restricted productions are return, throw, break, continue, and post-increment/decrement. In all cases, inserting semicolons does not fix the problem, but makes the parsed syntax clear, making the error easier to detect. return and throw take an optional value, while break and continue take an optional label. In all cases, the advice is to keep the value or label on the same line as the statement. This most often shows up in the return statement, where one might return a large object literal, which might be accidentally placed starting on a new line. For post-increment/decrement, there is potential ambiguity with pre-increment/decrement, and again it is recommended to simply keep these on the same line.

return a + b; // Returns undefined. Treated as: // return; // a + b; // Should be written as: // return a + b;


Comment syntax is the same as in C++
and many other languages.

// a short, one-line comment /* this is a long, multi-line comment about my script. May it one day be great. */ /* Comments /* may not be nested */ Syntax error */


Variables in standard JavaScript
have no type attached, and any value can be stored in any variable. Starting with ES6, the version of the language finalised in 2015, variables can be declared with let (for a block level variable), var (for a function level variable) or const (for an immutable one). However, while the object assigned to a const cannot be changed, its properties can. Before ES6, variables were declared only with a var statement. An identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript
is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Starting with JavaScript
1.5, ISO 8859-1 or Unicode
letters (or uXXXX Unicode
escape sequences) can be used in identifiers. In certain JavaScript
implementations, the at sign (@) can be used in an identifier, but this is contrary to the specifications and not supported in newer implementations.


Variables are lexically scoped at function level (not block level as in C), and this does not depend on order (forward declaration is not necessary): if a variable is declared inside a function (at any point, in any block), then inside the function, the name will resolve to that variable. This is equivalent in block scoping to variables being forward declared at the top of the function, and is referred to as hoisting.

However, the variable value is undefined until it is initialized, and forward reference is not possible. Thus a var x = 1 statement in the middle of the function is equivalent to a var x declaration statement at the top of the function, and an x = 1 assignment statement at that point in the middle of the function – only the declaration is hoisted, not the assignment.

Function statements, whose effect is to declare a variable of type Function and assign a value to it, are similar to variable statements, but in addition to hoisting the declaration, they also hoist the assignment – as if the entire statement appeared at the top of the containing function – and thus forward reference is also possible: the location of a function statement within an enclosing function is irrelevant.

Block scoping can be produced by wrapping the entire block in a function and then executing it — this is known as the immediately-invoked function expression pattern. Or by declaring the variable using the let keyword.


Variables declared outside any function are global . If a variable is declared in a higher scope, it can be accessed by child functions.

When JavaScript
tries to RESOLVE an identifier, it looks in the local function scope. If this identifier is not found, it looks in the outer function that declared the local one, and so on along the scope chain until it reaches the global scope where global variables reside. If it is still not found, JavaScript
will raise a ReferenceError exception.

When ASSIGNING an identifier, JavaScript
goes through exactly the same process to retrieve this identifier, except that if it is not found in the global scope, it will create the "variable" as a property of the global object. As a consequence, a variable never declared will be global, if assigned. Declaring a variable (with the keyword var) in the global code (i.e. outside of any function body), assigning a never declared identifier or adding a property to the global object (usually window) will also create a new global variable.

Note that JavaScript's strict mode forbids the assignment of an undeclared variable, which avoids global namespace pollution. Also const cannot be declared without initialization.


Here are examples of variable declarations and scope:

var x = 0; // A global variable, because it is not in any function function f() { var z = 'foxes', r = 'birds'; // 2 local variables m = 'fish'; // global, because it wasn't declared anywhere before function child() { var r = 'monkeys'; // This variable is local and does not affect the "birds" r of the parent function. z = 'penguins'; // Closure: Child function is able to access the variables of the parent function. } twenty = 20; // This variable is declared on the next line, but usable anywhere in the function, even before, as here var twenty; child(); return x; // We can use x here, because it is global } f(); console.log(z); // This line will raise a ReferenceError exception, because the value of z is no longer available

for (let i=0;iv * t * e



* JSHint * JSLint


* CoffeeScript * Dart * Emscripten * Google Closure Compiler * Google Web Toolkit * Morfik * TypeScript
* AtScript * Opa * Haxe * ClojureScript


* Ajax * Client-side * Dynamic HTML * JavaScript
library * JavaScript
syntax * Unobtrusive JavaScript


* Firebug * Komodo IDE * Microsoft Script Debugger
* Microsoft Script Editor * Opera Dragonfly * Venkman * Web Inspector


* JSDoc


* Ace

* Cloud9 IDE

* Atom

* CodeMirror

* Light Table * Brackets

* Koding * Orion

* Visual Studio

* Visual Studio Express

* Visual Studio Code * Visual Studio Team Services


* Comparison of layout engines * List of ECMAScript engines * List of JavaScript


* Comparison of JavaScript
frameworks * List of JavaScript


* Cascading Style Sheets
Cascading Style Sheets
* Document Object Model
Document Object Model
* WebAssembly


* npm * NuGet


* Active Server Pages * CommonJS * JSGI * Node.js
* Wakanda


* Jasmine * Mocha * QUnit * List of JavaScript
unit testing frameworks


* Douglas Crockford
Douglas Crockford
* Brendan Eich
Brendan Eich
* J