Types

There are two data types in JavaScript: primitives and objects. The following are the primitive types:

  • boolean
  • string
  • number
  • undefined
  • null
  • symbol
Primitive vs. Object

Primitive values are atomic data that are passed by value and compared by value. Objects, on the other hand, are compound pieces of data that are passed by reference and compared by reference.

typeof operator

You can check the type of a value by using the typeof operator

console.log(typeof "two");
console.log(typeof 2);
console.log(typeof true);
console.log(typeof undeclaredVariable);
console.log(typeof { value: 2 });

See typeof on MDN web docs for more details.

boolean

The Boolean type is similar to most other programming languages with two options: true or false.

let isRequired = true;
let isOptional = false;

See Boolean on MDN web docs for more details.

string

To be discussed shortly.

number

To be discussed shortly.

Primitive types and their Wrappers

The three primitive types string, number and boolean have corresponding types whose instances are objects: String, Number, Boolean.

const name = "Ali";
const firstname = new String("Ali");

console.log(name);
console.log(firstname);

console.log(typeof name);
console.log(typeof firstname);

console.log(name instanceof String);
console.log(firstname instanceof String);

You can use the instanceof operator to check the type of an object.

undefined and null

JavaScript has two "bottom" values!

  • An uninitialized variable is undefined.

  • The null value denotes an "intentionally" absent value.

Although you can, but don't deliberately set a value to undefined

JavaScript has lots of quirks and a bunch of them are around null and undefined (and how they behave, relate and differ). We will see some of these in later sections. For now, enjoy this!

console.log(typeof undefined);
console.log(typeof null);

For more details, Brandon Morelli has a nice article: JavaScript — Null vs. Undefined. For a complete reference, visit null and undefined on MDN web docs.

Symbol

Symbol is a new addition to the JavaScript language which enables Metaprogramming. It is beyond the scope of this course. You can consult the following resources if you are interested to learn more.