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

Expressions & Operators

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

Reading view

← Back to the full Chapter 5 deck

Expressions & Operators
Fullscreen for presentation · Print / PDF for a handout
1 / 3
Lesson

Expressions & Operators

Arithmetic, assignment, comparison, logical and string operators — and type coercion.

Expressions & Operators

Expressions & Operators

An expression is any combination of values, variables and operators that computes to a value.

Operator typePurpose
ArithmeticMathematical calculations on numeric values (+ - * / %).
AssignmentAssign values to variables (= += -=).
ComparisonCompare values, returning true/false (== === != < >).
LogicalCombine boolean expressions (&& || !).
String+ (concatenation) joins strings together.

Operator examples

// String concatenation with "+"
var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + " " + txt2;   // "John Doe"

// Assignment operator "+="
var txt = "What a very ";
txt += "nice day";              // "What a very nice day"

// Mixing numbers and strings
var x = 5;
var y = 5;
var z = x + y;        // 10 (number + number)
var s = x + "5";      // "55" (number + string)
var h = "Hello" + x;  // "Hello5"

Type coercion: when you add a string and a number, JavaScript converts the number to a string and concatenates them — a common source of bugs!

Expressions & Operators

Activity — complete the code

Complete the operators.

// String concatenation
var txt3 = txt1 " " + txt2; // "John Doe"
 
// Assignment (append)
txt "nice day";
 
// Arithmetic
var z = x y; // add
 
// Strict equality (value AND type)
if (age 18) { }