Skip to main content
Albero Academy

JavaScript Fundamentals

Variables, data types, operators and control flow — the core of the language every website runs on.

Chapter 1JavaScript15 min readBeginner friendly
  • #JavaScript
  • #Basics

HTML structures a page and CSS styles it — JavaScript makes it do things. Clicks, form validation, live updates, animations, entire apps: all JavaScript. It's the only language that runs natively in every browser, and with Node.js it runs on servers too. This chapter covers the fundamentals that every later topic — React, Node, even data work — builds on.

What is JavaScript?

JavaScript is a high-level, dynamically-typed language. "Dynamically typed" means you don't declare a variable's type — a variable can hold a number now and a string later. That flexibility is convenient and, occasionally, a footgun; we'll flag the sharp edges as we go.

Running JavaScript

Two easy ways to run code while you learn:

  • Browser console — open DevTools (F12) → Console, and type. Instant feedback.
  • In a page — add a <script> tag, ideally just before </body>:
script in a page
<body>
<h1 id="title">Hello</h1>
<script src="app.js"></script>
</body>
app.js
console.log("Hello, Albero!"); // prints to the console

Variables: let & const

Declare variables with const (can't be reassigned) or let (can). Reach for const by default and only switch to let when you know the value must change. Avoid the old var — its scoping rules cause bugs.

const & let
const name = "Aanya"; // fixed reference
let score = 0; // will change
score = score + 10; // fine
// const name = "Rohan"; // ✗ TypeError: can't reassign a const

Data types

JavaScript has a small set of primitive types plus objects:

TypeExample
string"hello"
number42
booleantrue / false
nullIntentional "no value"
undefinedA variable not yet assigned
object{ }
array[1, 2, 3]

Template literals (backticks) let you embed variables directly with ${…} — far cleaner than string concatenation:

template literals
const name = "Aanya";
const raise = 40;
console.log(`${name} got a ${raise}% raise`); // Aanya got a 40% raise

Operators

Arithmetic (+ - * / %) works as you'd expect. The one rule to burn in: use strict equality ===, never ==. The double-equals version coerces types and produces surprises (0 == "" is true). Strict equality checks value and type:

strict equality & logic
5 === 5 // true
5 === "5" // false (number vs string)
5 == "5" // true ← avoid == entirely
true && false // AND → false
true || false // OR → true
!true // NOT → false
// Modern helpers
user ?? "guest" // ?? → use "guest" if user is null/undefined
user?.profile?.email // ?. → safely read nested props

Control flow

if / else and loops
const score = 82;
if (score >= 90) grade = "A";
else if (score >= 75) grade = "B";
else grade = "C";
// Loop a fixed number of times
for (let i = 0; i < 3; i++) {
console.log("Attempt", i + 1);
}
// Loop over items (the modern default)
const tools = ["Excel", "SQL", "Python"];
for (const tool of tools) {
console.log(tool);
}

For short conditions, the ternary operator is handy: const label = score >= 40 ? "Pass" : "Fail";.

Practice

In the browser console, create a const for your name and a let for a score. Write an if/else that logs a grade, then a for…of loop over an array of your favourite tools. Use a template literal to print each one in a sentence.

Key takeaways

  1. 01JavaScript adds behaviour to pages and also runs servers via Node.js.
  2. 02Default to const; use let only when a value must change; never use var.
  3. 03Template literals (backticks) embed variables with ${…}.
  4. 04Always compare with === (strict), never == (it coerces types and surprises you).
  5. 05Use for…of to loop items; ?? and ?. handle null/undefined cleanly.