← Learning Hub
Beginner

Data Types and Operators

Values have types. The most common are:

  • Number: 42, 3.5
  • String (text): "Hello" or 'Hello'
  • Boolean: true or false
  • undefined and null: "no value yet" and "deliberately empty"

The type tells JavaScript what a value is and what you can do with it. You can do maths with numbers, join or search strings, and use booleans to make decisions. Mixing types can surprise you: "5" + 2 gives "52" (text joined), but "5" * 2 gives 10. Values from input boxes are always strings, so convert them with Number() before doing maths.

typeof tells you a value's type, e.g. typeof 42 is "number". Template strings with backticks make joining easy: `Total: Rs ${total}`.

Operators work with values: + - * / for maths, % for the remainder, and + also joins strings. Comparisons such as ===, > and < give true or false.

Example
<!DOCTYPE html>
<html>
  <body>
    <pre id="out"></pre>
    <script>
      const lines = [];
      lines.push("10 + 3 = " + (10 + 3));
      lines.push("10 % 3 = " + (10 % 3));
      lines.push("'5' + 2 = " + ("5" + 2) + "  (text joined!)");
      lines.push("Number('5') + 2 = " + (Number("5") + 2));
      lines.push("5 === 5 is " + (5 === 5));
      lines.push("typeof 'hi' is " + typeof "hi");
      document.getElementById("out").textContent = lines.join("\n");
    </script>
  </body>
</html>
▶ Try it yourself
Always use === (strict equal) rather than ==. Strict comparison does not quietly convert types, which avoids surprising bugs.
Key points
  • Main types: number, string, boolean, undefined, null (plus objects and arrays).
  • Input values are strings - convert with Number().
  • Use ===, not ==.
  • Template strings: `Hello ${name}`.

📝 Test yourself

1. What is "5" + 2 in JavaScript?
2. Which comparison should you prefer?
3. Fill in the operator that gives the remainder:
10 3 // gives 1