Data Types in JavaScript
Explore the types of data in JavaScript and how to use them.
JavaScript, like any programming language, uses various data types to store and manipulate data. Understanding these data types is crucial for writing effective and bug-free code. In this article, you'll learn the different data types in JavaScript, how they work, and how you can use them in your programs.
Basic Data Types
JavaScript has several basic (also called primitive) data types. These include:
1. Number
The Number
type represents both integer (whole) and floating-point (decimal) numbers. JavaScript uses the IEEE 754 standard for representing numbers, which means it can handle values in the range of approximately -2^53 to 2^53.
The code snippet below shows a positive integer, a decimal and a negative integer being assigned to different variables:
let age = 25; // integer
let pi = 3.14; // floating-point number
let negative = -42; // negative number
2. String
The String
type is used for text. Strings are sequences of characters enclosed in single quotes (' '
), double quotes (" "
), or backticks (` `
).
Examples:
let greeting = "Hello, world!";
let name = 'Alice';
let template = `The sum is ${2 + 3}`; // Template literals allow embedded expressions
console.log(template); //output: The sum is 5
3. Boolean
The Boolean
type has only two values: true
or false
. It's commonly used in conditional statements and logical operations (you'll learn about conditional statements and logical operations in subsequent articles).
Examples:
let isActive = true;
let isLoggedIn = false;
4. Undefined
A variable that has been declared but not assigned a value has the type undefined
.
Example:
let myVar;
console.log(myVar); // Output: undefined
5. Null
The Null
type represents the intentional absence of any object value. Null
simply means empty. It is often used to indicate that a variable should have no value.
Example:
let empty = null;
6. Symbol
Introduced in ECMAScript 6 (ES6), a Symbol
is a unique and immutable (can't be altered) primitive value that can be used as the key of an object property.
Example:
let sym = Symbol('description');
7. BigInt
Also introduced in ES6, BigInt
is used for very large integers that exceed the safe integer limit for the Number
type (remember the Number
data type only handle values in the range of approximately -2^53 to 2^53). You can create a BigInt
by adding n
to the end of an integer.
Example:
let bigInt = 1234567890123456789012345678901234567890n;
Complex Data Types
JavaScript also includes complex data types, primarily objects and arrays.
1. Object
The Object
type is a collection of properties. An object is created using curly braces {}
and properties are key-value pairs. An object is an entity that contains properties describing that entity. In the example below, we have a person object with the following properties: name, age, and height.
Example:
let person = {
name: 'John',
age: 30,
height: '7cm'
};
2. Array
An Array
is a special type of object used for storing ordered collections of values. Arrays are created using square brackets []
. An array is similar to a set. It collection of elements/values that are similar.
Example:
let numbers = [1, 2, 3, 4, 5];
let mixedArray = ['Hello', 42, true, null, {}];
You'll learn more about objects and arrays in future articles. For now, this is all you need to know.
Type Conversion
JavaScript is a dynamically typed language, meaning variables can change types. This often involves type conversion, either implicitly (coercion) or explicitly.
Implicit Conversion
JavaScript automatically converts types in certain operations.
In the first example below, JavaScript automatically converts the number 5
to a string '5'
and then joins it with the other string '5'
which results in the value '55'
.
Example:
let result = '5' + 5; // '55' (string)
let result2 = '5' - 2; // 3 (number)
Explicit Conversion
You can explicitly convert types using functions like String()
, Number()
, and Boolean()
.
Examples:
let num = 10;
//converts the number to a string
let str = String(num); // '10'
//converts 0 to a boolean
let bool = Boolean(0); // false
//converts the string to a number
let n = Number('123'); // 123
Checking Data Types
To check the type of a variable, use the typeof
operator.
Examples:
console.log(typeof 42); //output: 'number'
console.log(typeof 'Hello'); //output: 'string'
console.log(typeof true); //output: 'boolean'
console.log(typeof undefined); //output: 'undefined'
console.log(typeof null); //output: 'object' (this is actually a historical bug in JavaScript)
console.log(typeof Symbol('sym')); //output: 'symbol'
console.log(typeof 123n); //output: 'bigint'
console.log(typeof {name: 'Alice'}); //output: 'object'
console.log(typeof [1, 2, 3]); //output: 'object'
Understanding data types in JavaScript is fundamental to mastering the language. It is a very very important concept. Knowing the differences between these types and how to use them appropriately will help you write more efficient and effective code.
Practice working with these types and explore their behavior in different contexts to deepen your understanding. To master JavaScript, you need to practice regularly because practice makes perfect (though you might never be perfect at JavaScript, lol). That's a wrap! See you in the next one!