Full Stack JS

Chapter 4
0%

Chapter 4: JavaScript Fundamentals

Bring your web pages to life — learn variables, logic, loops, functions, and data structures.

Variables
Data Types
Operators
Conditionals
Loops
Functions
Arrays
Objects
Quiz

Variables

Variables are like labeled jars in a kitchen. The label is the variable name, and the contents are the value. You can change what's inside the jar anytime, and you use the label to find what you need.

JavaScript has three ways to declare variables:

JavaScript — Variables & console.log
Always use const by default. Only switch to let when you know the value will change. Never use var in modern JavaScript — it has weird scoping rules that cause bugs.

Data Types

JavaScript has several built-in data types. Every value you work with is one of these types:

JavaScript — Data Types
TypeExampleDescription
String"hello"Text data
Number42, 3.14Integers and decimals
Booleantrue, falseYes/no, on/off
undefinedundefinedNo value assigned yet
nullnullIntentionally empty
Array[1, 2, 3]Ordered list of values
Object{name: "Alex"}Key-value pairs

Operators

JavaScript — Operators
Always use === (strict equality), never ==. The loose equality == does automatic type conversion that leads to weird bugs: "0" == false is true!

Conditionals (if/else)

Conditionals are like a traffic light. If the light is green, you go. If it's yellow, you slow down. If it's red, you stop. Your code makes decisions the same way — checking conditions and running different code for each case.
JavaScript — if/else Conditionals

Loops

A loop is like a washing machine cycle. It repeats the same action (wash, rinse, spin) until a condition is met (the timer runs out). In code, loops repeat a block of code until a condition becomes false.
JavaScript — Loops
In modern JavaScript, prefer for...of for looping over arrays and for...in for looping over object keys. The classic for (let i = 0; ...) loop is still useful when you need the index number.

Functions

A function is like a recipe. You write it once, then use it whenever you want. You can pass in different ingredients (arguments) and get a different dish (return value) each time.
JavaScript — Functions

Arrays

An array is like a numbered shopping list. Each item has a position (index) starting from 0. You can add items, remove items, search through the list, and transform every item at once.
JavaScript — Arrays & Array Methods

🎯 Challenge!

  1. Create an array of 5 names
  2. Use .map() to make them all uppercase (hint: name.toUpperCase())
  3. Use .filter() to keep only names longer than 4 characters

Objects

An object is like a contact card. Instead of numbered positions (like arrays), each piece of data has a name (key). A person's contact card might have keys like "name", "phone", "email" — each with its own value.
JavaScript — Objects
Objects are everywhere in JavaScript! DOM elements, API responses, configuration, even arrays — they're all objects under the hood. Mastering objects is the key to mastering JavaScript.

📝 Chapter 4 Quiz

1. Which keyword declares a variable that CANNOT be reassigned?

let
const
var
static

2. What is the difference between === and ==?

They are the same
=== checks value AND type, == only checks value
=== is slower than ==

3. What does [1,2,3].map(n => n * 2) return?

[2, 4, 6]
[1, 2, 3, 1, 2, 3]
6
[1, 2, 3]

4. Which is correct arrow function syntax?

function => (a, b) { return a + b }
const add = (a, b) -> a + b
const add = lambda(a, b): a + b
const add = (a, b) => a + b

5. What does [10, 20, 30][1] return?

10
20
30
undefined
← Chapter 3: Flexbox & Grid Chapter 5: Async JavaScript →