Skip to main content

JavaScript Data Types

By SamK
0
0 recommends
Category(s)
Topic(s)

JavaScript features 8 primary data types:

  • String
  • Number
  • BigInt
  • Boolean
  • Undefined
  • Null
  • Symbol
  • Object

JavaScript Strings 

A string, also known as a text string, consists of a sequence of characters, such as "Apple".

Strings are enclosed in quotation marks, and you can use either single (' ') or double (" ") quotes.

let fruit1 = "Apple"; 
let fruit2 = 'Banana'; 

You can include quotes within a string, as long as the inner quotes are different from the ones used to define the string itself.

let reply1 = 'That\'s okay';
let reply2 = "She goes by 'Anna'";
let reply3 = "They refer to him as \"Jack\"";  

JavaScript Numbers

In JavaScript, all numbers are stored as floating-point values, meaning they are represented in decimal format.

You can write numbers with or without a decimal point, depending on the level of precision required.

let number1 = 50.75;
let number2 = 50;
let number3 = 6.28;

Exponential Notation

Very large or very small numbers can be expressed using scientific (exponential) notation, which simplifies their representation.

let a = 456e4;
let b = 456e-4;

JavaScript BigInt

In JavaScript, all numbers are stored using a 64-bit floating-point format.

Introduced in ES2020, the BigInt data type allows for the storage of integers that exceed the size limits of regular JavaScript numbers.

let largeNumber = BigInt("987654321098765432109876543210");

JavaScript Booleans

A Boolean in JavaScript can only represent one of two possible values: true or false.

let isAdmin = true;
console.log(typeof isAdmin); // "boolean"

Booleans are commonly used to evaluate conditions in programming logic.

JavaScript Arrays

In JavaScript, arrays are defined using [].

Items within an array are separated by commas.

Example: JavaScript array having three elements (fruit names).

const fruits = ["Apple", "Banana", "Orange"];
console.log(fruits[1]); // Output: Banana

Array indexes in JavaScript start from zero, meaning the first element is at index [0], the second at [1], and so on.

JavaScript Objects

In JavaScript, objects are defined using curly braces {}.

The properties of an object are represented as key-value pairs, with each pair separated by a comma.

const fruit = {
  name: "Apple",
  color: "red",
  weight: 150,
  taste: "sweet"
};

console.log(fruit.name); // Output: Apple

The typeof Operator

The typeof operator is used to determine the data type of a variable.

It returns the type of a variable or an expression, providing information about its kind.

console.log(typeof 42);       // Output: "number"
console.log(typeof "Banana"); // Output: "string"

Undefined

In JavaScript, when a variable is declared but not assigned a value, it is automatically given the value undefined. Its type is also considered undefined.

let fruit;
console.log(fruit); // Output: undefined

A variable can be cleared by assigning it the value undefined. When this happens, its type also becomes undefined.

Empty Values

An empty value is distinct from undefined.

An empty string is a valid value with a defined type, even though it contains no characters.

let fruit = "";
console.log(fruit); // Output:

JavaScript Types are Dynamic

JavaScript is dynamically typed, meaning a single variable can store values of different data types at different times.

let result;         // Initially, result is undefined
result = 42;        // Now result is a Number
result = "Apple";   // Now result is a String
console.log(result); // Output: Apple

Questions & Answers