Variables, data types and operators

Store and work with values — numbers, text, true/false — and print results to the console.

Module 5: JavaScript fundamentals · Lesson 15 of 25 · 18 min read · Free

JavaScript is a programming language: you give the computer instructions, and it carries them out in order. Every browser runs it. Click Run:

const shop = "Mama Nkechi's Provisions";
let price = 9500;
let quantity = 2;
const total = price * quantity;

console.log(shop);
console.log("Total:", total);

console.log(...) prints values to the console — in these Try-it boxes, under the code; in a real page, in DevTools → Console. It's how you see what your code is doing.

Variables

A variable is a named box that holds a value.

  • const — the value won't be reassigned. Use it by default.
  • let — the value will change (a counter, a running total).
  • var — the old way. You'll see it in older code; don't use it in new code.
const vatRate = 0.075;
let cartCount = 0;
cartCount = cartCount + 1;   // allowed: let
// vatRate = 0.1;            // error: const can't be reassigned

Names: camelCase, descriptive (deliveryFee, not df), no spaces, can't start with a number.

Data types

TypeExamples
number9500, 0.075, -3
string (text)"Lagos", 'Abuja', ` Hello ${name} `
booleantrue, false
undefineda variable with no value yet
null“deliberately empty”
object / arraycollections — lesson 5.4

Operators

const price = 12500;
const vat = price * 0.075;               // * multiply, / divide, + add, - subtract
console.log("VAT:", vat);
console.log("With VAT:", price + vat);
console.log("Remainder of 17 / 5:", 17 % 5);  // % gives the remainder

const firstName = "Amina";
console.log("Hello, " + firstName + "!");     // + joins strings
console.log(`Hello, ${firstName}! Your total is ₦${price}.`);  // template literal

console.log("5" + 3);   // "53" — careful! a string plus a number joins them
console.log(Number("5") + 3);  // 8 — convert text to a number first

That "5" + 3 surprise matters: values from forms are always strings. Convert them with Number(...) before doing maths.

Useful built-ins

Math.round(11110.5)          // 11111
Math.max(0, 300 - 500)       // 0
(1234567).toLocaleString("en-NG")  // "1,234,567"
"  Lagos ".trim()            // "Lagos"
"Lagos".toLowerCase()        // "lagos"
"Mama Gold Rice".includes("Rice")  // true

Check yourself

Answer in your head first, then open each question to see the answer and why.

  1. Which keyword should you use by default for a variable that won't be reassigned?

    • A const
    • B let
    • C var
    • D static
    Show the answer

    A) const — const by default; let when the value changes.

  2. What is "5" + 3 in JavaScript?

    • A "53"
    • B 8
    • C An error
    • D 53 (a number)
    Show the answer

    A) "53" — + with a string joins text. Convert with Number() first.

  3. How do you print a value to the console?

    • A console.log(value)
    • B print(value)
    • C echo value
    • D alert.console(value)
    Show the answer

    A) console.log(value) — console.log is the standard way to inspect values.

More free lessons: What front-end developers do · Set up VS Code, Chrome and Live Server · Text, links and images · Selectors, colours and fonts · Mobile-first design and media queries · The DOM: find and change page elements · Git and GitHub