Data Types & Operators

🟨 JavaScript Lesson 3 Beginner

JavaScript is a dynamically-typed language. Variables can hold any data type and can change dynamically at runtime. The language includes primitives, objects, and implicit coercions.

1 Primitives vs Reference Types

Variables store primitives directly in memory stacks, while referencing locations for objects on heap segments:

  • Primitives: `string`, `number` (both integers and decimals are double-precision floats), `boolean`, `undefined` (declared but not assigned), `null` (explicit emptiness), `symbol`, `bigint`.
  • Reference Types: Objects, Arrays, and Functions.

Strict Comparison: Always use the strict equality operator (`===`) instead of loose equality (`==`). Loose equality performs implicit type coercion (casting), leading to unexpected results (e.g. `5 == "5"` is true, but `5 === "5"` is false).

2 Dynamic Casting & Operators

Let's run a program that demonstrates operators, typeof investigations, and strict comparison rules:

JavaScript — Types and Comparisons ▶ Run Code
let value = 42;
console.log("Type of 42: " + typeof value);

value = "Hello";
console.log("Type of 'Hello': " + typeof value);

// Comparisons
console.log("Loose matching (5 == '5'):", 5 == '5'); // true
console.log("Strict matching (5 === '5'):", 5 === '5'); // false

// Logical Operators
let isMember = true;
let score = 85;
let discount = (isMember && score > 80) ? "20%" : "0%";
console.log("Discount tier: " + discount);
3 Code Challenge
Challenge: Write a comparison check testing loose and strict equality between `null` and `undefined` (e.g. `null == undefined` and `null === undefined`). Print both results and explain the difference.