3. Modern JavaScript Fundamentals
JavaScript Basics: Variables, Types, Logic & Functions
Learn modern JavaScript from scratch: const vs let, data types, template literals, strict === comparisons, truthy/falsy rules, conditionals, and reusable arrow functions.
- Declare variables cleanly using const and let and recognize JavaScript primitive types
- Compare values reliably using strict equality (===) and logical operators
- Write clear conditionals and reusable functions with parameters, return values, and block scope
HTML builds structure and CSS handles layout and styling. JavaScript brings pages to life—responding to clicks, validating forms before they submit, updating prices dynamically, and communicating with backend servers.
Unlike HTML and CSS, JavaScript is a full programming language with variables, conditions, loops, and functions.
Variables: const vs let (and Avoiding Legacy var)
A variable acts like a labeled box holding a value in memory. Modern JavaScript gives you two clean keywords:
const(default choice): Creates a binding that cannot be reassigned later. Usingconstby default prevents accidental overwrites in larger files.let: Use only when a value genuinely needs to change over time—such as a score counter, loading state, or loop index.
const academyName = "Yadav Academy";
let completedLessons = 0;
completedLessons += 1;
console.log(`${academyName}: ${completedLessons} completed`);Wrapping strings in backticks (`...`) lets you embed variables directly inside ${...}. Developers call these template literals—and unlike Python f-strings, template literals can span multiple lines natively!
(You may see var in older tutorials from before 2015. Skip var in modern projects because its function-level scoping rules cause confusing bugs.)
JavaScript Data Types at a Glance
| Type | Examples | Plain-English Role |
|---|---|---|
string | "Alex", 'Web Dev' | Text wrapped in single, double, or backtick quotes |
number | 42, 3.14, -5 | Both whole integers and decimals share one number type |
boolean | true, false | Logical Yes/No flags (all lowercase!) |
undefined | let x; | Variable exists, no value assigned yet |
null | selectedUser = null; | Intentionally empty ("no value right now") |
Check any value's type anytime using typeof value.
Strict Equality (===), Conditionals & Truthy/Falsy Rules
JavaScript has two equality operators, and one causes endless beginner surprises:
- Loose equality (
==) converts types secretly behind your back ("5" == 5istrue, and"" == 0istrue!). - Strict equality (
===and!==) compares both value AND type with zero hidden surprises ("5" === 5isfalse).
Always write === and !==. Combine conditions using logical AND (&&), OR (||), and NOT (!):
const age = 20;
const hasTicket = true;
if (age >= 18 && hasTicket) {
console.log("Welcome inside!");
} else if (age >= 18) {
console.log("Please buy a ticket first.");
} else {
console.log("Adults only.");
}Truthy & Falsy Values
In JavaScript conditions, these six values automatically count as false (falsy): false, 0, "" (empty string), null, undefined, and NaN. Everything else—including "hello", 42, empty arrays [], and empty objects {}—is truthy!
Functions, Scope & Arrow Syntax
Functions bundle reusable steps under one clear name. An input enters through a parameter, and return sends a result back to the caller:
// Standard function declaration
function formatDuration(minutes) {
if (minutes < 60) {
return `${minutes} min read`;
}
const hours = (minutes / 60).toFixed(1);
return `${hours} hr course`;
}
// Concise arrow function (great for short one-line transforms)
const applyDiscount = (price, discount) => price - discount;
console.log(formatDuration(15)); // "15 min read"
console.log(applyDiscount(50, 10)); // 40Variables declared with const or let inside { ... } live only inside that block (block scope) so inner helper names never collide with variables outside the function.
Writing (x) => x * 2 returns x * 2 automatically (implicit return). The moment you add curly braces { ... }, you must write return: (x) => { x * 2 } returns undefined!
Try It Live: Grade Evaluator & Edge Cases
Experiment with changing scores, strict vs loose equality checks, and arrow functions in the console playground below:
Click “Run Code” to execute this snippet right in your browser.
Practice Challenge: Shipping Fee Calculator
Write an arrow function calculateShipping(orderTotal, isMember) that returns 0 if isMember is true or orderTotal >= 50, and returns 7.99 otherwise.
Quick Checkpoints
In modern JavaScript, which keyword should you default to when declaring variables?
What does (n) => { n * 2 } return when called with 5?