๐Ÿ“š Lecture Series ยท Lesson 01

Introduction to
JavaScript

// A complete beginner's guide to the language of the web

0% โ€” scroll to track progress

01

What is JavaScript?

JavaScript is the programming language of the Web. It is what makes websites interactive โ€” from clicking buttons and submitting forms, to live chat apps and browser-based games. Alongside HTML (structure) and CSS (style), JavaScript is the third pillar of front-end development.

๐ŸŒ
Runs in the Browser
Every modern browser has a JavaScript engine built in โ€” no installation needed to run your first script.
โšก
Interpreted Language
Code is executed line-by-line at runtime, making it fast to prototype and test without a compile step.
๐Ÿ”„
Dynamic & Flexible
Variables can hold any type of value. It supports functional, object-oriented, and event-driven paradigms.
๐Ÿ–ฅ๏ธ
Also Runs on Servers
With Node.js, JavaScript runs on servers too โ€” making it a full-stack language for front and back-end.
๐Ÿ’ก
Quick start: Open any browser, press F12, go to the Console tab, and type console.log("Hello, World!") โ€” you're now running JavaScript!
JavaScript
// Your very first JavaScript program
console.log("Hello, World!");
console.log("Welcome to JavaScript ๐Ÿš€");
OUTPUT
02

Variables

A variable is a named container that stores a value. Think of it as a labelled box you can put data into and retrieve later. In modern JavaScript, you declare variables using let, const, or the older var.

Keyword Reassignable? Scope Use When
const No Block Value won't change (preferred)
let Yes Block Value may change
var Yes Function Legacy code (avoid in new code)
JavaScript
const name = "Alice";      // string โ€” won't change
let age = 25;              // number โ€” might change
let job = "Developer";    // can be reassigned

job = "Engineer";         // reassigning let is fine

console.log(name);
console.log(age);
console.log(job);
OUTPUT
โš ๏ธ
Rule of thumb: Always use const by default. Only switch to let when you know the value needs to change. Never use var in new code.
03

Data Types

Every value in JavaScript has a type. There are 7 primitive types plus the special Object type (which includes arrays and functions).

JavaScript
const greeting = "Hello";        // String
const score     = 42;             // Number
const isReady   = true;           // Boolean
let   nothing;                     // undefined (no value assigned)
const empty     = null;            // null (intentionally empty)
const colors    = ["red", "blue"]; // Array (a type of Object)

console.log(typeof greeting);  // "string"
console.log(typeof score);     // "number"
console.log(typeof isReady);   // "boolean"
console.log(typeof nothing);   // "undefined"
console.log(typeof empty);     // "object" (historical quirk!)
console.log(typeof colors);    // "object"
OUTPUT
โœ…
Template literals let you embed variables directly into strings using backticks and ${"{}"}: `Hello, ${"{name}"}!` โ€” far cleaner than string concatenation.
JavaScript โ€” Template Literals
const name = "Alice";
const age  = 25;

// Old way (messy):
// "Hello, " + name + "! You are " + age + " years old."

// Modern way (clean):
const message = `Hello, ${name}! You are ${age} years old.`;
console.log(message);
OUTPUT
What will typeof 42 return in JavaScript?
JavaScript has only one numeric type โ€” number โ€” for both integers and decimals.
04

Operators

Operators let you perform calculations, compare values, and combine logic. The most common categories are arithmetic, comparison, and logical.

JavaScript โ€” Arithmetic
const a = 10, b = 5;

console.log(a + b);   // 15  โ€” addition
console.log(a - b);   // 5   โ€” subtraction
console.log(a * b);   // 50  โ€” multiplication
console.log(a / b);   // 2   โ€” division
console.log(a % b);   // 0   โ€” modulo (remainder) โ†’ 10%5=0, 10%3=1
console.log(a ** b);  // 100000 โ€” exponentiation (10^5)
OUTPUT
JavaScript โ€” Comparison & Equality
console.log(10 >  5);        // true  โ€” greater than
console.log(10 <  5);        // false โ€” less than
console.log(10 >= 10);       // true  โ€” greater than or equal

// === checks value AND type (always prefer this)
console.log(5  === 5);       // true
console.log(5  === "5");     // false! number โ‰  string
console.log(5  ==  "5");     // true  โ€” == coerces types (avoid!)
OUTPUT
โš ๏ธ
Always use === (triple equals) for comparisons. The double-equals == performs type coercion which leads to subtle, hard-to-find bugs.
04b

Logical Operators, Falsy Values & Nullish

JavaScript has powerful operators for combining conditions and handling missing values. To use them confidently, you first need to understand falsy values โ€” the handful of values JavaScript treats as false in a boolean context.

JavaScript โ€” Falsy Values
// These 6 values are FALSY โ€” everything else is truthy
const falsyValues = [0, "", false, null, undefined, NaN];

falsyValues.forEach(v => {
  console.log(`${v} is falsy`);
});

console.log("---");

// Truthy examples โ€” watch out for these surprises!
const truthyValues = [42, "hello", [], {}];
truthyValues.forEach(v => {
  console.log(`${JSON.stringify(v)} is TRUTHY`);
});
OUTPUT
โš ๏ธ
Common surprises: An empty array [] and empty object {} are truthy โ€” only an empty string "" and zero 0 are falsy. This trips up almost every beginner.

&& (AND) and || (OR) don't just return true or false โ€” they return one of their actual operands. This makes them incredibly useful for short-circuit evaluation and default values.

JavaScript โ€” && and || Operators
console.log("--- AND (&&) ---");
// && returns the FIRST falsy value, or the last value if all truthy
console.log(false  && "hello");   // false  โ€” stops at first falsy
console.log("hello" && "world");   // "world" โ€” both truthy, returns last

console.log("--- OR (||) ---");
// || returns the FIRST truthy value, or the last value if all falsy
console.log("hello" || "world");   // "hello" โ€” first truthy wins
console.log(0       || "Guest");   // "Guest" โ€” 0 is falsy, falls through
console.log(""      || "Guest");   // "Guest" โ€” "" is falsy, falls through
OUTPUT
JavaScript โ€” Practical Short-Circuit Patterns
// || for default values (classic pattern)
const greet = (name) => console.log(`Hello, ${name || "Guest"}!`);
greet("Alice");   // Hello, Alice!
greet("");        // Hello, Guest!  โ€” empty string is falsy

// && for conditional execution โ€” "do this only if that is true"
const user = { name: "Alice", isAdmin: true };
user.isAdmin && console.log("User is an admin");

const loggedIn = false;
loggedIn && console.log("Dashboard loaded");
console.log("(nothing logged โ€” user not logged in)");
OUTPUT

?? (Nullish Coalescing) solves a key weakness of ||: it only falls back when the left side is null or undefined โ€” not when it's 0 or "", which are legitimate values.

JavaScript โ€” ?? Nullish Coalescing
const scores = { alice: 10, bob: 0 };  // Bob scored zero โ€” valid!

console.log("--- Using || (problem) ---");
console.log(scores.alice || "N/A");   // 10  โ† fine
console.log(scores.bob   || "N/A");   // "N/A" โ† BUG! 0 is falsy

console.log("--- Using ?? (solution) ---");
console.log(scores.alice ?? "N/A");   // 10  โ† fine
console.log(scores.bob   ?? "N/A");   // 0   โ† correct!
// ?? only triggers for null / undefined โ€” not 0 or ""
OUTPUT

?. (Optional Chaining) lets you safely access deeply nested properties without crashing when an intermediate value is null or undefined. Instead of an error, it returns undefined.

JavaScript โ€” ?. Optional Chaining
const users = [
  { name: "Alice", address: { city: "Warsaw" } },
  { name: "Bob"   }   // no address!
];

// Without optional chaining โ€” would CRASH on Bob:
// console.log(users[1].address.city) โ†’ TypeError!

// With ?. โ€” safe, returns undefined instead of crashing
console.log(users[0]?.address?.city);   // "Warsaw"
console.log(users[1]?.address?.city);   // undefined โ€” no crash!
console.log(users[5]?.address?.city);   // undefined โ€” index out of bounds, no crash!

console.log("--- Combined with ?? ---");
// ?. and ?? are best friends โ€” access safely, then provide fallback
console.log(users[0]?.address?.city ?? "Unknown city");  // Warsaw
console.log(users[1]?.address?.city ?? "Unknown city");  // Unknown city

// Also works for optional method calls
const phone = users[1]?.contact?.getPhone?.() ?? "no phone on file";
console.log(phone);
OUTPUT
โœ…
Quick decision guide:
Use || when any falsy value should trigger the fallback (e.g. empty string, 0).
Use ?? when only null / undefined should trigger it โ€” 0 and "" are valid.
Use ?. whenever a property might not exist to avoid TypeErrors.
What does 0 ?? "default" return?
?? only falls back on null or undefined. Since 0 is neither, it is returned as-is. Compare with 0 || "default" which would return "default" because 0 is falsy.
05

Conditionals

Conditionals let your program make decisions. The if / else if / else structure executes different blocks of code depending on whether a condition is true or false.

JavaScript โ€” if / else
const score = 87;

if (score >= 90) {
  console.log("Excellent! You got an A.");
} else if (score >= 75) {
  console.log("Great score! You passed with distinction.");
} else if (score >= 50) {
  console.log("You passed.");
} else {
  console.log("Try again next time.");
}
OUTPUT

For simple value-matching, switch is often cleaner than a long chain of else if statements:

JavaScript โ€” switch
const day = "Monday";

switch (day) {
  case "Monday":
    console.log("Start your week strong! ๐Ÿ’ช");
    break;
  case "Friday":
    console.log("Almost the weekend! ๐ŸŽ‰");
    break;
  default:
    console.log("Just another day.");
}
OUTPUT
What does the break keyword do inside a switch statement?
Without break, execution "falls through" into the next case โ€” a common source of bugs for beginners!
06

Loops

Loops let you repeat a block of code without writing it multiple times. JavaScript offers several looping constructs โ€” choose the one that fits your situation.

JavaScript โ€” for loop
// Classic for loop: init ; condition ; increment
for (let i = 1; i <= 5; i++) {
  console.log(`Lap ${i}`);
}
OUTPUT
JavaScript โ€” while & for...of
// while loop โ€” runs as long as condition is true
let count = 3;
while (count > 0) {
  console.log(`Countdown: ${count}`);
  count--;
}
console.log("Blast off! ๐Ÿš€");

console.log("---");

// for...of โ€” best way to loop over arrays
const fruits = ["Apple", "Banana", "Cherry"];
for (const fruit of fruits) {
  console.log(fruit);
}
OUTPUT
07

Functions

A function is a reusable block of code that performs a specific task. Instead of writing the same code over and over, you define it once in a function and call it whenever needed. Functions are the foundation of organized, maintainable code.

JavaScript โ€” Function Declaration
// Define once...
function greet(name) {
  return `Hello, ${name}!`;
}

// ...call many times
console.log(greet("Alice"));
console.log(greet("Bob"));
OUTPUT

Modern JavaScript also supports arrow functions โ€” a shorter, cleaner syntax especially popular for small, inline functions:

JavaScript โ€” Arrow Functions
// Traditional function:
// function square(n) { return n * n; }

// Arrow function โ€” same thing, shorter:
const square = (n) => n * n;

// With multiple lines, use curly braces + return:
const circleArea = (radius) => {
  const pi = 3.14159;
  return pi * radius ** 2;
};

console.log(square(5));
console.log(circleArea(5).toFixed(2));
OUTPUT
What does the return keyword do inside a function?
A function without return returns undefined by default. Use return when you want the function to produce a value you can use elsewhere.
08

Live Playground

Time to practice! Write your own JavaScript code below and run it. Try combining variables, a function, and a loop to see everything working together.

โšก Interactive Editor
// Output will appear here...
๐ŸŽฏ
Challenge: Modify the playground to print the first 10 numbers of the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34โ€ฆ
โœ“

Lesson Summary

You've covered the core building blocks of JavaScript. Here's what you now know:

What JavaScript is and where it runs
const, let variable declarations
Primitive data types & typeof
Template literals with ${"{}"}
Arithmetic & comparison operators
Falsy values: 0, "", false, null, undefined, NaN
&& and || short-circuit evaluation
?? nullish coalescing & ?. optional chaining
if / else if / else logic
switch statements
for, while, for...of loops
Function declarations & arrow functions
๐Ÿš€
What's next? Lesson 02 covers Arrays & Objects in depth, along with powerful built-in methods like .map(), .filter(), and .reduce() that make working with data elegant and expressive.
๐Ÿ“„

Official ECMAScript Specification

JavaScript is built on top of ECMAScript โ€” the official language standard maintained by Ecma International. The specification precisely defines how the language works under the hood. As you grow as a developer, reading the spec becomes an invaluable skill for understanding edge cases and language behaviour.

โšก
Living Specification (Latest Draft)
The most up-to-date version of ECMA-262, continuously updated with finished proposals. This is the go-to reference for current JavaScript behaviour.
โ†’ tc39.es/ecma262
๐Ÿ“˜
ECMA-262 Official Standard (Ecma International)
The formal published edition of the ECMAScript Language Specification โ€” ECMA-262 โ€” hosted on the Ecma International website.
โ†’ ecma-international.org
๐Ÿ“–
Note for beginners: The ECMAScript spec is a dense, formal document written for language implementers โ€” not beginners. Don't be discouraged if it feels overwhelming at first. Come back to it once you're comfortable writing JavaScript day-to-day. For now, MDN Web Docs is the more beginner-friendly reference.