Lesson 4 +10 XP

JavaScript Syntax

JavaScript Syntax

JavaScript syntax is the set of rules for how to write JavaScript code.

Statements

Programs are made of statements, which are instructions the computer runs one by one:

let x = 5;
let y = 6;
let z = x + y;

Semicolons

Statements often end with a semicolon ;. Semicolons separate statements and make code predictable. Most programmers always use them.

Values

JavaScript has two kinds of values:

  • Literals: fixed values like 5 or "Hello"
  • Variables: values stored in names, created with let or const

Identifiers

Names for variables, functions, and labels are called identifiers:

  • Can contain letters, digits, _, and $
  • Must start with a letter, _, or $
  • Are case sensitive: x and X are different
  • Cannot use reserved words like let or function

Case sensitivity

let Name = "Ada";
let name = "Bob";

Name and name are two completely different variables.

Comments

Comments are ignored by JavaScript:

// This is a single line comment

/* This is a
   multi-line comment */

TL;DR

  • Code is made of statements, usually ending with ;.
  • Use let and const to create variables.
  • Identifiers start with a letter, underscore, or $.
  • JavaScript is case sensitive.
  • Use // and / / for comments.