JavaScript Data Types
Learn JavaScript through interactive, bite-sized lessons. Practice with real code challenges and build projects step-by-step.
Start JavaScript Journey →JavaScript is a dynamically typed language, which means variables can hold different types of data. Understanding these data types is crucial for effective programming.
Primitive Data Types
JavaScript has six primitive data types:
- Number: Represents both integer and floating-point numbers.
- String: Represents textual data.
- Boolean: Represents true or false values.
- Undefined: Represents a variable that has been declared but not assigned a value.
- Null: Represents a deliberate non-value or absence of any object value.
- Symbol: Represents a unique identifier (introduced in ECMAScript 6).
Number
Numbers in JavaScript can be integers or floating-point values. There's no separate integer type.
let integerNumber = 42;
let floatingPointNumber = 3.14;
let scientificNotation = 1.23e5; // 123000
String
Strings are sequences of characters enclosed in single or double quotes.
let singleQuoted = 'Hello, World!';
let doubleQuoted = "JavaScript is awesome!";
let templateLiteral = `You can use ${singleQuoted} in template literals`;
Boolean
Booleans represent true or false values, often used in conditional statements.
let isTrue = true;
let isFalse = false;
let comparison = 5 > 3; // true
Undefined and Null
These types represent the absence of a value, but in different ways:
let undefinedVariable;
console.log(undefinedVariable); // undefined
let nullValue = null;
console.log(nullValue); // null
Object Data Type
Objects are collections of key-value pairs and can include functions (methods). Arrays and functions are also objects in JavaScript.
let person = {
name: "John",
age: 30,
isStudent: false
};
let colors = ["red", "green", "blue"];
function greet(name) {
return `Hello, ${name}!`;
}
Type Checking
You can use the typeof operator to check the type of a value:
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (this is a known quirk)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
Type Coercion
JavaScript performs automatic type conversion in certain situations. This can lead to unexpected results if not handled carefully.
console.log("5" + 3); // "53" (string concatenation)
console.log("5" - 3); // 2 (numeric subtraction)
console.log("5" == 5); // true (loose equality)
console.log("5" === 5); // false (strict equality)
Best Practices
- Use strict equality (
===) to avoid unexpected type coercion. - Be explicit about type conversions using functions like
Number(),String(), orBoolean(). - Use
typeoffor type checking, but be aware of its limitations (e.g., withnull). - Consider using let and const for variable declarations to improve code clarity and prevent unintended reassignments.
Understanding JavaScript data types is fundamental to writing robust and error-free code. It's closely related to JavaScript variables and JavaScript operators, which you might want to explore next.