ArticleslgStudy

science

JavaScript syntax

JavaScript syntax is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand JavaScript syntax rather than just read about it. In short: 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.

JavaScript syntax — main illustration
JavaScript syntax — illustration

Key takeaways

  • JavaScript syntax belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect JavaScript syntax to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of JavaScript syntax from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

JavaScript syntax: A snippet of JavaScript code with keywords highlighted in different colors
A snippet of JavaScript code with keywords highlighted in different colors
JavaScript syntax: JavaScript code which displays weekdays in a browser
JavaScript code which displays weekdays in a browser

Worked examples

Example 1 — a first encounter with JavaScript syntax

Start with the simplest possible case. Write down what JavaScript syntax claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to JavaScript syntax before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about JavaScript syntax ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of JavaScript syntax

In research
JavaScript syntax appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses JavaScript syntax in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
JavaScript syntax is common in secondary-school and first-year university syllabi. It links to neighbouring topics JavaScript, Programming language syntax, so understanding it makes those chapters shorter.
In everyday life
Look for JavaScript syntax outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “JavaScript syntax” →

How to study JavaScript syntax in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what JavaScript syntax means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain JavaScript syntax out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is JavaScript syntax in simple terms?

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.

Why does JavaScript syntax matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study JavaScript syntax?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on JavaScript syntax.

Tags

  • JavaScript
  • Programming language syntax

Keep exploring