JavaScript
Hi!, Let's talk about JavaScript, the world's most popular programming language. In this guide, I’ll explain the different data types in JavaScript and show you some real-world examples.
- Datatype in JavaScript
JavaScript has two main categories of data types: Primitives and Non-Primitives (also called reference types). First, let's explore the primitive data types.
Primitives
JavaScript has seven primitive data types:
number
string
boolean
null
undefined
symbol
bigint
1. Numbers
The numbers (type number) it is the data type more basic exist on JavaScript. Furthermore, in JavaScript whole numbers and floating point values, all numbers declared of the data type number.
For example:
// Integers 2 42 -1 // Floating-point numbers 3.14 0.001 // Exponential notation 1.23e8 // 123,000,000 // Special number cases Infinity -Infinity NaN // "Not-a-Number", occurs in invalid mathematical operations
Use case:
Numbers are often used in mathematical calculations, financial systems, or any other context where you’re working with quantitative data.
let price = 49.99; let tax = price * 0.2; console.log(tax); // Outputs: 9.998
2. String
The string type is used to represent textual data. Strings in JavaScript are created using single quotes ('), double quotes ("), or backticks (`) for template literals
'Hello, World!' "He said, 'JavaScript is awesome!'" `Template literals allow embedding expressions: 5 + 5 = ${5 + 5}`
Use case:
Strings are used for text manipulation, user input, and displaying messages.
let greeting = "Hello"; let name = "Alice"; console.log(`${greeting}, ${name}!`); // Outputs: Hello, Alice!
3. Boolean
A boolean represents one of two values: true or false. It’s commonly used in control flow statements, conditional logic, and flags.
true false
Use case:
Booleans are used in situations where you need to track a binary state (e.g., on/off, true/false, yes/no).
let isLoggedIn = true; if (isLoggedIn) { console.log("Welcome back!"); } else { console.log("Please log in."); }
4. Null
null is a special primitive value that represents the intentional absence of any object value. It's often used to indicate that a variable should have no value.
let emptyValue = null;
Use case:
null
is typically used to explicitly indicate that a variable is empty or should be reset.
let user = { name: "John" }; user = null; // The user object is now cleared.
5. Undefined
undefined is a primitive type that means a variable has been declared but has not yet been assigned a value.
let notAssigned; console.log(notAssigned); // Outputs: undefined
Use case:
undefined
is often seen in cases where variables are declared but not initialized.
function getUserAge(user) { let age; if (user.age !== undefined) { age = user.age; } return age; }
6. Symbol
Symbol is a unique and immutable primitive value, typically used as keys for object properties to avoid name conflicts. Every Symbol
is completely unique.
let symbol1 = Symbol('description'); let symbol2 = Symbol('description'); console.log(symbol1 === symbol2); // Outputs: false
Use case:
Symbols are useful when creating private or unique keys for object properties.
7. Bigint
BigInt is a numeric data type that can represent integers with arbitrary precision. This allows you to work with very large numbers beyond the safe limit of the number type
let bigNumber = 1234567890123456789012345678901234567890n; let anotherBigNumber = BigInt(98765432101234567890);
Use case:
BigInt
is useful when working with large numbers, such as in cryptography, or scientific calculations.
let big1 = 9007199254740991n; let big2 = 2n; console.log(big1 + big2); // Outputs: 9007199254740993n
Non-Primitives (Reference Types)
In addition to primitives, JavaScript has non-primitive types that are treated as references. The most important ones are:
- Object
- Array
- Function
1. Object
Objects are collections of key-value pairs. The keys are strings (or symbols), and the values can be any type.
let person = { name: "John", age: 30, isEmployee: true }; console.log(person.name); // Outputs: John
2.Array
Arrays are ordered collections of elements, typically used to store lists of data.
let numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); // Outputs: 1
3.Function
Functions are reusable blocks of code that can be called with different inputs to perform a specific task.
function add(a, b) { return a + b; } console.log(add(2, 3)); // Outputs: 5
This guide gives you an overview of the main data types in JavaScript, from numbers and strings to complex objects and functions. Understanding these types will help you work effectively with JavaScript in any context!