Before we start working with data in JavaScript, it’s important to understand data types. Every piece of information you use in JavaScript falls into a specific category, known as a data type.
JavaScript is a dynamically typed language, which means you don’t have to specify a variable’s type when declaring it—it can hold different types of data at different times. For example:
let value = 42; // Number
value = "Hello"; // Now it's a String
This flexibility is powerful but can also lead to unexpected behaviour if not handled properly. That’s why understanding JavaScript data types is crucial for writing reliable code.
Let’s explore the different types of data in JavaScript and how they work.
String: Represents text, enclosed in quotes (e.g., “Hello”, ‘World’).
Number: Represents both integers and floating-point numbers (e.g., 42, 3.14).
Boolean: Represents true or false values.
Undefined: A variable that has been declared but not assigned a value.
Null: Represents an intentional absence of value.
Symbol: A unique and immutable value, often used as object keys.
BigInt: Used for very large integers (e.g., 9007199254740991n).
Object: A collection of key-value pairs (e.g., { name: “Alice”, age: 25 }).
Array: An ordered list of values (e.g., [1, 2, 3]).
Function: A reusable block of code (e.g., function greet() { console.log(“Hello”); }).
Date: Represents a date and time (e.g., new Date()).
RegExp: Represents a regular expression pattern (e.g., /abc/).
Hoisting is a JavaScript mechanism where variable declarations are moved to the top of their scope during compilation.
var variables are hoisted and initialised as undefined.
let and const variables are hoisted but remain uninitialised (they are in a “temporal dead zone” until their declaration).
typeof: Used to check the data type of a variable (e.g., typeof “Hello” returns “string”).
instanceof: Used to check if an object is an instance of a specific class (e.g., [] instanceof Array).
Implicit Conversion: JavaScript automatically converts values (e.g., “5” + 3 results in “53”).
Explicit Conversion: Using functions like Number(), String(), Boolean() to manually convert data types. For example:
Number("42") → 42
String(100) → "100"
Boolean(1) → true
Primitive data types (String, Number, Boolean, etc.) are immutable (cannot be changed once created).
Reference data types (Objects, Arrays, Functions) are mutable (values can be changed). For example:
let str = "Hello";
str[0] = "J"; // No effect, because strings are immutable.
let arr = [1, 2, 3];
arr[0] = 10; // Works, because arrays are mutable.
JavaScript is dynamically typed, meaning variables can change their type during execution. For example:
let myVar = "Hello"; // String
myVar = 42; // Now a Number
Always declare variables with let or const instead of var.
Use const for values that shouldn’t change.
Use typeof to check variable types before operations.
Avoid implicit type conversion to prevent unexpected results.
Here’s an example demonstrating different data types:
const name = "Alice"; // String
const age = 25; // Number
const isStudent = true; // Boolean
const hobbies = ["reading", "gaming", "coding"]; // Array
const user = { name: "Alice", age: 25 }; // Object
console.log(typeof name); // "string"
console.log(typeof age); // "number"
console.log(typeof isStudent); // "boolean"
console.log(typeof hobbies); // "object"
console.log(typeof user); // "object"
Ready to start mastering JavaScript variables? Explore more tutorials on HowCodingWorks and elevate your coding skills!