Data Types - potatoscript/javascript GitHub Wiki
π Data Types in JavaScript π’
In JavaScript, data types refer to the kinds of values that can be stored in variables. Understanding data types is essential for writing effective and error-free code, as it allows you to manipulate and work with different kinds of data in your programs.
There are two main categories of data types in JavaScript:
- Primitive Data Types: These are basic data types that hold a single value.
- Reference Data Types: These are more complex data types that refer to objects, arrays, and functions.
Letβs break down each data type in detail:
π 1. Primitive Data Types π€
Primitive data types are simple and represent a single value. In JavaScript, there are seven primitive data types:
-
String π¬
-
A string is used to represent text in JavaScript. Strings are enclosed in either single quotes (
' ') or double quotes (" "). -
Example:
let greeting = "Hello, world!"; let name = 'Lucy'; -
Important: Strings are immutable, meaning once you create a string, you canβt change the characters inside it. You can, however, create a new string by modifying the original.
-
-
Number π’
-
Number is used to represent both integer and floating-point numbers (decimals).
-
Example:
let age = 25; // An integer let price = 19.99; // A decimal number (float) let temperature = -10; // Negative number -
Important: JavaScript has only one type of number β
Numberβ for both integers and floating-point numbers.
-
-
Boolean π
-
Boolean is a simple data type that can only be one of two values:
trueorfalse. Booleans are typically used to represent the truth or falsehood of a condition. -
Example:
let isAdult = true; // True let isOnline = false; // False -
Important: Booleans are often used in conditional statements like
ifstatements.
-
-
Null π«
-
Null is a special value that represents the intentional absence of any object value. It is a non-existent or empty value.
-
Example:
let person = null; -
Important:
nullis a deliberate assignment, indicating that no value is assigned.
-
-
Undefined π
-
Undefined means a variable has been declared but has not been assigned a value. It is JavaScriptβs default value for uninitialized variables.
-
Example:
let car; // Variable is declared, but not initialized console.log(car); // undefined -
Important:
undefinedis different fromnullbecauseundefinedmeans the variable is not yet assigned a value, whilenullexplicitly means there is no value.
-
-
Symbol π
-
Symbol is a unique and immutable primitive value that is often used to create unique identifiers for object properties.
-
Example:
const uniqueID = Symbol('id'); console.log(uniqueID); // Symbol(id) -
Important: Symbols are often used in scenarios where uniqueness is required, like object property keys.
-
-
BigInt π₯
-
BigInt is a relatively new data type used to represent large integers that cannot be stored in the regular
Numbertype. It is used for numbers beyond the range of theNumbertype. -
Example:
const bigNumber = 9007199254740991n; // BigInt is created by appending "n" to the number console.log(bigNumber); // 9007199254740991n -
Important: BigInt is useful for scenarios that need precise large integer calculations.
-
π 2. Reference Data Types π
Reference data types are more complex and can hold multiple values or properties. They are stored by reference, meaning the variable stores a reference (memory address) to the data.
-
Object ποΈ
-
An object is a collection of properties and methods, where each property is a key-value pair. Objects are used to store more complex data, like multiple related pieces of information.
-
Example:
let person = { name: "Lucy", age: 25, isStudent: true }; -
Important: Objects can store multiple values, and you access them using the key names (e.g.,
person.name).
-
-
Array π
-
An array is a special variable that holds a list of values. Arrays can hold values of any data type and can be indexed using numbers (starting from 0).
-
Example:
let fruits = ["apple", "banana", "cherry"]; let numbers = [10, 20, 30, 40]; -
Important: Arrays allow you to store collections of similar or different values, and you can access them using their index number (e.g.,
fruits[1]gives"banana").
-
-
Function π§βπ»
-
A function is a block of code designed to perform a particular task. Functions can accept parameters (input), perform some operations, and return a result.
-
Example:
function greet(name) { return "Hello, " + name + "!"; } let message = greet("Lucy"); console.log(message); // "Hello, Lucy!" -
Important: Functions are first-class objects, meaning they can be stored in variables, passed as arguments, and returned from other functions.
-
π Data Type Conversion π
JavaScript automatically converts between data types when needed, but you can also manually convert between types.
-
String to Number: You can convert a string to a number using the
Number()function.let str = "123"; let num = Number(str); // 123 (as a number) -
Number to String: You can convert a number to a string by using the
String()function or concatenating with an empty string.let num = 123; let str = String(num); // "123" (as a string) -
Boolean to String: Convert a boolean to a string using the
String()function.let bool = true; let str = String(bool); // "true" -
String to Boolean: You can convert strings to booleans using the
Boolean()function.let str = "hello"; let bool = Boolean(str); // true (non-empty strings are truthy)