The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program. The examples below make use of the console.log() function present in most browsers for standard text output. The JavaScript standard library lacks an official standard text output function (with the exception of document.write). 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 it is not commonly used. TypeScript, which extends JavaScript with type annotations and additional features, has the same syntax as well as its own additional features.
Origins
Brendan Eich summarized the ancestry of the syntax in the first paragraph of the JavaScript 1.1 specification as follows:
JavaScript 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. JavaScript syntax is mostly derived from Java syntax, which in turn is derived from C syntax and C++ syntax.
Basics
Keywords
Reserved keywords The following words are keywords:
Reserved keywords in strict mode The following words are keywords in strict mode:
Reserved keywords in module code or async function body The following words are keywords in module code or async function body:
Literal values The following words refer to literal values used by the language:
JavaScript also defines the following global constants, which are not keywords:
Removed keywords The following words, primarily associated with Java, were removed from the ECMAScript 5/6 standard:
TypeScript keywords The additional following words are keywords in TypeScript:
Case sensitivity JavaScript is case sensitive. It is common to start the name of a constructor with a capitalized letter, and the name of a function or variable with a lower-case letter. Example:
Whitespace and semicolons Unlike in C, whitespace in JavaScript source can directly impact semantics. Semicolons end statements in JavaScript. Because of 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 "[", slash "/", plus "+", and minus "-". Of these, the open parenthesis is common in the immediately invoked function expression pattern, and open bracket occurs sometimes, while others are quite rare. An example:
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 '[', so the line is not accidentally joined with the previous one. This is known as a defensive semicolon, and is employed because code may otherwise become ambiguous when it is rearranged. For example:
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. In such a case where perhaps unusual semicolon placement occurs, it may just be better to manually place semicolons at the end of statements. 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.
Comments Comment syntax is the same as in C, C++ and other programming languages. Single-line comments begin with // and continue until the end of the line. Multi-line comments start with /* and end with */ , and can span multiple lines. A third type of comment, the hashbang comment, starts with #! and continues until the end of the line. They are only valid at the start of files and are intended for use in CLI environments.
Variables
Variables in standard JavaScript have no type attached, so any value (each value has a type) can be stored in any variable. Starting with ES6, the 6th version of the language, variables could be declared with var for function scoped variables, and let or const which are for block level variables. Before ES6, variables could only be declared with a var statement. Values assigned to variables declared with const cannot be changed, but their properties can. var should no longer be used since let and const are supported by modern browsers. A variable's identifier must start with a letter, underscore (_), or dollar sign ($), while subsequent characters can also be digits (0-9). JavaScript is case sensitive, so the uppercase characters "A" through "Z" are different from the lowercase characters "a" through "z". 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.
… excerpt ends here. Continue reading the full article.


