JavaScript includes these primitive types:
stringnumberbooleannullundefinedsymbolbigintlet name = "Alice"; // string
let age = 30; // number
let isOnline = true; // boolean
let score = null; // null
let value; // undefined
let id = Symbol("id"); // symbol
let big = 123456789012345678901234567890n; // bigintObjects, arrays, and functions are reference types:
let person = { name: "Bob", age: 25 }; // object
let colors = ["red", "green"]; // array
function greet() { return "Hi"; } // functiontypeof checks the data type:
typeof "hello" // "string"
typeof 42 // "number"
typeof true // "boolean"
typeof undefined // "undefined"
typeof null // "object" (legacy quirk)
typeof {} // "object"
typeof [] // "object"
typeof function(){} // "function"null and undefined have specific meanings:
let a = null; // intentional empty
let b; // undefined
console.log(b); // undefined
let result = "abc" * 3;
console.log(result); // NaN (Not a Number)JavaScript allows type conversion:
String(123) // "123"
Number("45") // 45
Boolean(0) // false
Boolean("text") // true
// Implicit:
let x = "5" * 2; // 10 (string converted to number)
let y = "5" + 2; // "52" (number converted to string)=== for strict comparisonsnull and undefined before using variablesAsk the AI if you need help understanding or want to dive deeper in any topic