JavaScript Data Types – Overview of JS Types
JavaScript has a variety of data types that are essential to understand, especially for beginners. Here’s an overview of the main data types in JavaScript:
1. Primitive Data Types
Primitive data types are the most basic types of data. They include:
- String: Used for text. Strings are written inside quotes (
" "
,' '
, or backticks` `
).- Example:
"Hello, World!"
- Example:
- Number: Represents both integer and floating-point numbers.
- Example:
42
,3.14
- Example:
- Boolean: Represents a logical entity and can have two values:
true
orfalse
.- Example:
true
,false
- Example:
- Undefined: A variable that has been declared but not assigned a value.
- Example:
let x; console.log(x); // undefined
- Example:
- Null: Represents the intentional absence of any object value.
- Example:
let y = null; console.log(y); // null
- Example:
- Symbol: Represents a unique identifier.
- Example:
let sym = Symbol('description');
- Example:
- BigInt: Used for numbers larger than the
Number
type can safely handle.- Example:
let bigIntNum = 1234567890123456789012345678901234567890n;
- Example:
2. Non-Primitive Data Types (Objects)
Objects are collections of properties and methods.
- Object: Used to store collections of data or more complex entities.
- Example:
let person = { firstName: "John", lastName: "Doe", age: 30 };
- Example:
- Array: A special type of object used to store multiple values in a single variable.
- Example:
let fruits = ["Apple", "Banana", "Cherry"];
- Example:
- Function: A block of code designed to perform a particular task.
- Example:
function greet() { console.log("Hello, World!"); }
- Example:
3. Special Objects
- Date: Represents date and time.
- Example:
let date = new Date();
- Example:
- RegExp: Represents regular expressions.
- Example:
let pattern = /codeformate/i;
- Example:
4. Type Checking
You can check the type of a variable using the typeof
operator.
- Example:
console.log(typeof "Hello"); // "string" console.log(typeof 42); // "number" console.log(typeof true); // "boolean"
Understanding these basic data types and their usage is fundamental to writing and understanding JavaScript code.