JavaScript data types, JavaScript string number boolean object
Primitive Data Types
- String –
"Hello" - Number –
42,3.14 - Boolean –
trueorfalse - Undefined – declared but no value assigned
- Null – intentional empty value
- Symbol – unique identifiers
- BigInt – for very large integers
- (11) YouTube
- Build a To-Do List App in Python – Console-Based Project for Beginners – 804
Non-Primitive Data Types
- Object
javascriptCopyEditlet person = { name: "Ali", age: 25 };
- Array
javascriptCopyEditlet fruits = ["apple", "banana"];
- Function
javascriptCopyEditfunction greet() { console.log("Hi!"); }
Type Conversion
Implicit:
javascriptCopyEditconsole.log("5" + 2); // "52"
Explicit:
javascriptCopyEditNumber("5"); // 5
String(123); // "123"
typeof Operator
javascriptCopyEdittypeof "Hello"; // string
typeof 42; // number
🌐 JavaScript Data Types: Full Guide
JavaScript is a dynamically typed language, meaning you don’t need to specify variable types. The type is determined automatically at runtime.
🔹 1. Primitive Data Types
Primitive types are the most basic data types in JavaScript. They are immutable and represent a single value.
🟡 a. Number
- Represents both integer and floating-point numbers.
- Includes special values:
Infinity,-Infinity, andNaN(Not-a-Number).
let age = 25;
let price = 19.99;
let temperature = -5;
let notANumber = 0 / 0; // NaN
🟡 b. String
- Represents a sequence of characters.
- Enclosed in single (
'), double ("), or backticks (`\) for template literals.
let name = "Alice";
let greeting = 'Hello';
let message = `Welcome, ${name}!`; // Template literal
🟡 c. Boolean
- Represents one of two values:
trueorfalse.
let isLoggedIn = true;
let hasPermission = false;
🟡 d. Undefined
- A variable that has been declared but not assigned a value is
undefined.
let x;
console.log(x); // undefined
🟡 e. Null
- Represents an intentional absence of any value.
let selectedItem = null;
Note: typeof null returns "object" – this is a well-known quirk in JavaScript.
🟡 f. Symbol (ES6)
- Represents a unique and immutable identifier.
- Often used to create hidden object properties.
let sym1 = Symbol("id");
let sym2 = Symbol("id");
console.log(sym1 === sym2); // false
🟡 g. BigInt (ES11)
- Used for arbitrarily large integers beyond the safe limit of the
Numbertype.
let bigNumber = 9007199254740991n;
let anotherBig = BigInt("123456789012345678901234567890");
🔸 2. Non-Primitive (Reference) Data Types
These types can store collections of values or more complex entities. They are mutable and stored by reference.
🔷 a. Object
- The most important complex data type in JavaScript.
- Stores key-value pairs.
let person = {
name: "John",
age: 30,
isEmployed: true
};
🔷 b. Array
- A special kind of object used for storing ordered collections.
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4, 5];
🔷 c. Function
- A function is also an object in JavaScript.
function greet(name) {
return `Hello, ${name}`;
}
Or as an expression:
let greet = function(name) {
return `Hi, ${name}`;
};
Or with arrow syntax:
let greet = (name) => `Hey, ${name}`;
🔷 d. Date, RegExp, Map, Set, WeakMap, WeakSet, etc.
These are built-in object types in JavaScript.
Date: Work with dates and times.RegExp: Pattern matching with regular expressions.Map: Key-value pairs with any data type as keys.Set: Stores unique values.WeakMapandWeakSet: Similar to Map and Set but with weak references (used for memory management).
let now = new Date();
let pattern = /hello/i;
let map = new Map();
map.set("key1", "value1");
🧠 Dynamic Typing Example
JavaScript allows you to change the type of a variable during runtime:
let data = 42; // number
data = "hello"; // string
data = true; // boolean
🔍 typeof Operator
Use typeof to check the type of a variable:
console.log(typeof 123); // "number"
console.log(typeof "abc"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (quirk)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"
📌 Summary Table
| Type | Category | Example | typeof result |
|---|---|---|---|
Number | Primitive | 42, 3.14, NaN | "number" |
String | Primitive | "hello", 'world' | "string" |
Boolean | Primitive | true, false | "boolean" |
Undefined | Primitive | let x; | "undefined" |
Null | Primitive | null | "object" |
Symbol | Primitive | Symbol("id") | "symbol" |
BigInt | Primitive | 12345678901234567890n | "bigint" |
Object | Reference | { name: "Alice" } | "object" |
Array | Reference | [1, 2, 3] | "object" |
Function | Reference | function() {} | "function" |
✅ Best Practices
- Use
constorletinstead ofvar. - Avoid comparing different data types with
==; use===(strict equality). - Prefer using
Array.isArray()instead oftypeoffor arrays. - Be cautious with
nullandundefined.
