Chapter 1 deck Chapter 2 deck Chapter 3 deck Chapter 4 deck Chapter 5 deck Chapter 6 deck Chapter 7 deck Chapter 8 deck Chapter 9 deck Chapter 10 deck Chapter 11 deck Chapter 12 deck Chapter 13 deck Chapter 14 deck

Statements, Variables & Comments

5 slides · use ←/→ or Space to navigate, F for fullscreen.

Reading view

← Back to the full Chapter 5 deck

Statements, Variables & Comments
Fullscreen for presentation · Print / PDF for a handout
1 / 5
Lesson

Statements, Variables & Comments

How JavaScript programs are built, how to store data, and how to document code.

Statements, Variables & Comments

JavaScript Statements

A JavaScript statement is an instruction that tells the browser what to do. A program is simply a series of statements. A semicolon separates each statement.

Statements are made up of values (variables, numbers, strings), keywords, operators and expressions.

var x = 5;      // statement 1
var y = 6;      // statement 2
var z = x + y;  // statement 3
Statements, Variables & Comments

JavaScript Variables

Naming rules

  • Names are case-sensitive (myVar ≠ MyVar).
  • Must begin with a letter, underscore (_) or dollar sign ($).
  • Cannot use reserved JavaScript keywords.
  • Use camelCase by convention: firstName, totalAmount.
// Using var (classic)
var strName = "value";

// Without var (global scope — avoid!)
strName = "value";

// Modern alternatives (ES6+)
let x = 10;       // block-scoped, reassignable
const PI = 3.14;  // block-scoped, constant
var firstname;
firstname = "Toot";
document.write(firstname);
firstname = "Tweet";   // outputs: Toot
Statements, Variables & Comments

JavaScript Comments

Comments are lines that are not executed. They explain code or temporarily disable it during debugging.

// This is a single-line comment
var x = 5; // you can also comment after code

/*
  This whole block is a comment
  and will not execute.
*/
Statements, Variables & Comments

Activity — complete the code

Complete the variable declarations and the statement terminator.

x = 10; // block-scoped, reassignable
PI = 3.14; // block-scoped, constant
name = "Toot"; // classic declaration
 
var total = 5 6; // each statement ends with this