JavaScript + Node.js track

Lesson 02 · Part 1: Basics

Variables & Types

`let`/`const`, primitive types, `typeof` and type conversion

Part 1: Basics2 min readFree lesson

Step: Basics | Player: 01_JS_Getting Started

Learning Objectives

  • Explains the differences between let, const, and var and allows you to choose correctly.
  • You can distinguish between the seven primitive types of JavaScript.
  • The result of explicit/implicit type conversion can be predicted.
  • You can distinguish between truthy and false values and use them in conditional expressions.
  • You can check the type of value with the typeof operator.

1. Variable declaration: const, let, var

const cannot be reassigned, let can be reassigned, and var is not used in modern code due to function scope and hoisting issues. The default is const, and let is used only when the value changes.

const PI = 3.14;
let count = 0;
count += 1;

2. Primitive types

There are seven primitive types in JavaScript: string, number, boolean, null, undefined, symbol, and bigint. Compare/copy. as a value itself, as opposed to an object

typeof "hi";        // "string"
typeof 42;          // "number"
typeof true;        // "boolean"
typeof undefined;   // "undefined"
typeof null;        //"object" ← historical bug

3. Type conversion (Type coercion)

Explicit conversion is performed automatically by operators such as Number(), String(), and Boolean(), and implicit conversion is performed automatically by operators such as + and ==. Explicit conversion is recommended because unintentional conversion can cause bugs.

Number("42");   // 42
String(42);     // "42"
"5" + 1;        //"51" (string concatenation)
"5" - 1;        //4 (number conversion)

4. truthy / falsy

There are only 7 values that evaluate to falsy in the conditional expression: false, 0, -0, 0n, "", null, undefined, and NaN. Everything else is truthy.

if ("") console.log("Not running");
if ("0") console.log("Executed - non-empty string");

Core example

file Contents covered
01_let_const.js Reassign/Scope difference between const and let
02_primitives.js 7 primitive types and typeof
03_conversion.js explicit/implicit type conversion comparison
04_truthy_falsy.js truthy/falsy behavior in conditional expressions

⚠️ Common Mistakes

  1. A misunderstanding that the properties of an object declared as const cannot be changed (reassignment is not possible, but internal changes are possible).
  2. Incorrectly checking for null without knowing it was typeof null === "object".
  3. I know that parseInt("10px") is 10, but I am confused that Number("10px") is NaN.
  4. Traps of ==, such as "" == false and 0 == false. (Use === if possible)
  5. When let is sufficient, but var is used to create a hoisting bug.

❓ Frequently Asked Questions (FAQ)

Q1. Do I always have to use const first? A. Yes. Change to let only when reallocation is necessary. Intention becomes clear.

Q2. What is the difference between null and undefined? A. undefined indicates "no value has been assigned", null indicates "intentionally empty".

Q3. When is BigInt used? A. Used when an integer exceeding Number.MAX_SAFE_INTEGER (2^53 - 1) is required. It is unnecessary for general arithmetic.

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/01_let_const.jsJavaScript
/** Compare the difference in operation between const and let.*/
const name = "Ada";
let age = 30;

age += 1;
console.log(`${name}, ${age}`);

//The interior of a const object is mutable.
const user = { name: "Linus" };
user.name = "Grace";
console.log("user:", user);

try {
  //user = {};  // Reallocation is not possible - TypeError
} catch (e) {
  console.error(e.message);
}
src/02_primitives.jsJavaScript
/** Check the 7 primitive types with typeof.*/
const samples = [
  "hello",
  42,
  true,
  undefined,
  null,
  Symbol("id"),
  10n,
];

for (const v of samples) {
  console.log(String(v).padEnd(15), "→", typeof v);
}
src/03_conversion.jsJavaScript
/** Compare explicit/implicit type conversion.*/
console.log(Number("42"));      // 42
console.log(Number("42px"));    // NaN
console.log(parseInt("42px"));  // 42

console.log(String(123));       // "123"
console.log(Boolean(0));        // false

//implicit conversion
console.log("5" + 1);   // "51"
console.log("5" - 1);   // 4
console.log("5" * "2"); // 10
console.log(+"3.14");   // 3.14
src/04_truthy_falsy.jsJavaScript
/** Check 7 false values.*/
const falsyValues = [false, 0, -0, 0n, "", null, undefined, NaN];

for (const v of falsyValues) {
  console.log(`${String(v).padEnd(10)} → ${Boolean(v)}`);
}

//common pitfalls
console.log("'0' →", Boolean("0"));   //true (non-empty string)
console.log("[] →", Boolean([]));     //true (object)
console.log("{} →", Boolean({}));     // true

Assignment

homework_01.js

  • Put at least 6 different type values into an array, pair each value with the typeof result, and output it.
  • Also include null, and comment out what typeof null is when printing.

homework_02.js

  • Convert string input given as a constant to a number.
  • If conversion fails, “Not a number” is output.
  • If successful, whether Even/odd is output.
Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

homework_01.jsJavaScript
/** Outputs typeof results of various types.*/
const values = ["text", 3.14, true, undefined, null, Symbol("k"), 99n, { a: 1 }];

for (const v of values) {
  //Note: typeof null === "object" is the result of a bug left over from the early days of the language.
  console.log(`${String(v).padEnd(12)} → ${typeof v}`);
}
homework_02.jsJavaScript
/** Convert strings to numbers to determine even/odd numbers.*/
const input = "42";

const n = Number(input);
if (Number.isNaN(n)) {
  console.log("not a number");
} else if (n % 2 === 0) {
  console.log(`${n} is an even number`);
} else {
  console.log(`${n} is odd`);
}

Learn this lesson with a mentor

Work through the JavaScript + Node.js track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now