JavaScript Fundamentals / JavaScript Basics

Syntax and Structure

🈯️ Syntax and Structure of JavaScript

JavaScript syntax defines the rules for writing JavaScript code, including how whitespace is used, how statements are written, how identifiers are named, how expressions are formed, and the role of reserved keywords.


🧱 Whitespace

Whitespace refers to characters that provide the space between other characters. JavaScript has the following whitespace:

  • Carriage return

  • Space

  • New Line

  • tab

The following JavaScript code doesn’t use whitespace:

let formatted = true; if (formatted) {console.log('The code is easy to read');}

It is equivalent to the following code that uses whitespace. Hence, this code is much easier to read:

let formatted = true;

if (formatted) {
  console.log('The code is easy to read');
}

🏷️ Statements

A statement is a piece of code that either declares a variable or instructs the JavaScript engine to perform a task. A simple statement ends with a semicolon (;).

let message = "Welcome to JavaScript";
console.log(message);

🧩 Blocks

A block is a sequence of zero or more simple statements. A block is delimited by a pair of curly brackets {}. For example:

if (window.localStorage) {
  console.log('The local storage is supported');
}

💠Identifiers

An identifier is a name you choose for variables, parameters, functions, classes, etc.

  • Must start with a letter (a–z or A–Z), underscore _, or dollar sign $.

  • Can include letters, digits (0–9), underscores _, and dollar signs $ .


🖇️ Expressions

An expression is a piece of code that evaluates to a value. For example:

6 + 10

The above expression returns sixteen.


🪄Keywords (Reserved words)

JavaScript defines a list of reserved keywords that have specific uses. You cannot use the reserved keywords as identifiers.

Some of the Keywords are :

break

case

catch

continue

debugger

default

else

export

extends

function

if

import

new

return

super

throw

try

null