Introduction
JavaScript, is a popular scripting language for web development, works with various kinds of information, known as data types. It's crucial for any web developer to understand this properly as data types lays the foundation of our code or program.
In this article, we'll learn about JavaScript data types, from the basics, like numbers and strings, to more complex structures.
Primitive Data Types
In JavaScript, the simplest forms of data are known as primitive data types. These are the types that hold a single value.
Number
This type is used for any kind of numerical value, be it an integer (like 5 or -12) or a decimal (which we call a floating-point number, like 3.14). Whether you're counting items or calculating scores, numbers are your go-to in JavaScript.
Example
let age = 21; // An integer
let price = 19.99; // A floating-point number
String
Strings are sequences of characters, usually representing text. Anything inside quotes is a string, like "Hello, world!" or 'coding is fun'. Strings can include letters, numbers, spaces, and symbols.
Example
let greeting = 'Hello, world!'; // Using single quotes
let name = "Jane Doe"; // Using double quotes
let message = `Welcome, ${name}!`; // Using backticks for template literals
Boolean
This type has just two possible values: true or false. Booleans are often used in decision-making in code, like checking if a condition is met.
Example
let isStudent = true;
let hasPassed = false;
Undefined
If a variable has been declared but not assigned a value, its type is undefined. It's like a placeholder waiting to be filled.
Example
let result;
console.log(result); // Shows "undefined"
Null
This type represents "no value" or "nothing". It's used to intentionally indicate that a variable is empty or unknown.
Example
let emptyValue = null;
Symbol
Introduced in the newer versions of JavaScript, symbols are unique and immutable, often used to create private object properties or to add unique property keys to objects.
Example
let id = Symbol('id');
BigInt
For numbers larger than the Number type can hold, BigInt comes into play. It can represent very large integers, handy for high precision calculations.
Example
let largeNumber = BigInt(9007199254740991);