Values and Types

Numbers, strings, booleans, null and undefined, checked with typeof. Then see what + and * do when a string meets a number.

JavaScript · beginner · 6 min · 10 steps · free

What you'll learn

Types.js

const age = 36;
const name = 'Ada';
const isAdmin = false;
const nothing = null;
let notSet;
console.log(typeof age, typeof name, typeof isAdmin);
console.log(typeof nothing, typeof notSet);
const joined = '5' + 3;
const product = '5' * 3;
console.log(joined, product);

Step by step

  1. age is created with the value 36.
  2. name is created with the value "Ada".
  3. isAdmin is created with the value false.
  4. nothing is null: "no value", set on purpose.
  5. notSet was never given a value, so it is undefined.
  6. It prints number string boolean.
  7. typeof null says "object". This is an old mistake in JavaScript that can never be fixed; null is not an object.
  8. + with a string joins text: "5" + 3 becomes "53".
  9. * only works on numbers, so JavaScript turns "5" into 5: 15.
  10. It prints 53 15.

Common mistakes

Best practices

Key takeaways

Check yourself

typeof "hi"?

Answer: "string"

"3" * "4"?

Answer: 12. * converts both to numbers.

·

More in The Basics