Data Types

JavaScript data types

JavaScript has 6 data types:

  • Numbers
  • String (text)
  • Boolean
  • Undefined
  • Null
  • Objects

Numbers

Numbers are any number without quotes, including numbers with decimals.

For example: -10, 258, 3.14.

Strings

Strings are characters wrapped in single or double quotes.

Example:

let str = "Hello World";

Undefined

undefined represents a lack of defined value.

Variables declared but not initialized to a value will have the value undefined.

let x;
console.log(typeof x); // undefined

Null

null represents the intentional absence of a value. So it’s empty, and there’s nothing in it.

const x = null;

Boolean

Boolean has 2 values: true or false.

In Javascript, 0, empty strings, undefined, null, and NaN (Not-A-Number) are all considered false values.

const isGood = true;

Objects

An object is the collection of related data.

let dog = {
  name: "Puppy",
  age: 2,
  color: "gray",
};

name, age, and color are the properties of the dog object. We access the values of these properties using the dot or bracket notation.

// Using dot
console.log(dog.name); // Puppy

// Using bracket
console.log(dog["age"]); // 2