BeginnerJavaScript · Lesson 2 of 7

Variables, Types & Strings

let and const, numbers, strings, booleans, null and undefined, template literals, and === vs ==.

Declare variables with const (cannot be reassigned) or let (can change). Prefer const; never use the old var.

The basic types are number (integers and decimals together), string, boolean, undefined (no value assigned yet) and null (deliberately empty). typeof tells you a value's type.

Template literals use backticks and ${...} to insert values into text. Always compare with === and !==: the loose == converts types first and gives surprising results such as "5" == 5 being true. Convert text to numbers explicitly with Number().

basics.jsJavaScript
const school = "Azania Secondary";
let students = 420;
students += 15;

const fee = 150000.5;
const isOpen = true;
let nickname;              // undefined
const guardian = null;     // deliberately empty

console.log(typeof school, typeof students, typeof isOpen, typeof nickname);

const name = "  amina juma  ";
const clean = name.trim().toUpperCase();
console.log(clean, clean.length, clean.split(" "));

const average = (78 + 64 + 91) / 3;
console.log(`${school}: ${students} students, average ${average.toFixed(1)}`);

console.log("5" == 5, "5" === 5);      // true false  ->  always use ===
console.log("10" + 5, Number("10") + 5); // "105" 15  ->  convert text first
console.log(0.1 + 0.2);                  // 0.30000000000000004 (floating point!)
console.log(fee.toLocaleString("en-US"), guardian ?? "no guardian listed");
Runs in your browser · JavaScript

Key points

  • const by default, let when a value changes, never var.
  • Always use === / !==; convert strings with Number() before arithmetic.
  • Template literals — backticks with ${value} inside — are the clearest way to build text.

Exercise

In the console or a file, store three test scores as strings (as if typed by a user), convert them to numbers, and print the total and the average to two decimal places using a template literal.

Show solution

Try the exercise yourself first — then compare your approach with this one.

Form inputs and prompt() give you strings, so convert each with Number() before adding — otherwise + joins the text. toFixed(2) formats the average with two decimals.

scores.jsJavaScript
const typed = ["78", "64", "91"];          // what a user would type
const scores = typed.map(Number);          // [78, 64, 91]

const total = scores[0] + scores[1] + scores[2];
const average = total / scores.length;

console.log(typed[0] + typed[1]);          // "7864"  <- strings are joined, not added
console.log(`Total: ${total}, average: ${average.toFixed(2)}`);   // Total: 233, average: 77.67
Runs in your browser · JavaScript

Check your understanding

  1. What does "10" + 5 evaluate to in JavaScript?

  2. Which comparison is true?

  3. What is the value of a variable declared with let x; before anything is assigned?

  4. Which declaration should you avoid in modern JavaScript?

Ask AI