JavaScript Data Types

Understand the common types of values used in JavaScript.

A data type tells JavaScript what kind of value a variable stores.

Common JavaScript Data Types

String

A string stores text inside quotes.

JavaScript
let message = "Welcome to JavaScript";

Number

A number stores a whole number or decimal value.

JavaScript
let price = 499;
let rating = 4.5;

Boolean

A boolean stores either true or false.

JavaScript
let isLoggedIn = true;

Undefined

undefined means a variable has no assigned value.

JavaScript
let username;

console.log(username);

Output:

JavaScript
undefined

Null

null is an intentionally empty value.

JavaScript
let selectedProduct = null;

Checking a Data Type

Use typeof to check the data type of a value.

JavaScript
let age = 25;

console.log(typeof age);

Output:

JavaScript
number

Key Takeaways

  • String stores text.
  • Number stores whole numbers and decimals.
  • Boolean stores true or false.
  • undefined and null represent missing or empty values.
  • Use typeof to check a value's data type.