JavaScript
// Primitive Data Types
console.log("=== Primitive Types ===");
// Number
let age = 25;
let price = 99.99;
console.log("Number:", age, typeof age);
console.log("Float:", price, typeof price);
// String
let name = "John";
let message = 'Hello World';
let template = `Name: ${name}`;
console.log("String:", name, typeof name);
console.log("Template Literal:", template);
// Boolean
let isActive = true;
let isComplete = false;
console.log("Boolean:", isActive, typeof isActive);
// Undefined
let undefinedVar;
console.log("Undefined:", undefinedVar, typeof undefinedVar);
// Null
let nullVar = null;
console.log("Null:", nullVar, typeof nullVar);
// Symbol (ES6)
let sym = Symbol("id");
console.log("Symbol:", sym, typeof sym);
// BigInt (ES2020)
let bigNumber = 9007199254740991n;
console.log("BigInt:", bigNumber, typeof bigNumber);
// Reference Types
console.log("\n=== Reference Types ===");
// Object
let person = {
name: "John",
age: 25
};
console.log("Object:", person, typeof person);
// Array
let numbers = [1, 2, 3, 4, 5];
console.log("Array:", numbers, typeof numbers);
// Function
function greet() {
return "Hello";
}
console.log("Function:", greet, typeof greet);Output
=== Primitive Types ===
Number: 25 number
Float: 99.99 number
String: John string
Template Literal: Name: John
Boolean: true boolean
Undefined: undefined undefined
Null: null object
Symbol: Symbol(id) symbol
BigInt: 9007199254740991n bigint
=== Reference Types ===
Object: { name: 'John', age: 25 } object
Array: [ 1, 2, 3, 4, 5 ] object
Function: [Function: greet] functionThis program demonstrates all data types available in JavaScript.
Primitive Data Types
Primitives are immutable values stored directly in memory:
-
Number: Integers and floating-point numbers
typeof 42→"number"- Includes
Infinity,-Infinity,NaN
-
String: Text data
- Single quotes:
'text' - Double quotes:
"text" - Template literals:
text
- Single quotes:
-
Boolean:
trueorfalse- Used in conditionals and logic
-
Undefined: Variable declared but not assigned
let x; console.log(x);→undefined
-
Null: Intentional absence of value
typeof null→"object"(historical bug)
-
Symbol: Unique identifier (ES6)
- Used for object property keys
-
BigInt: Large integers (ES2020)
- Append
nto number:9007199254740991n
- Append
Reference Data Types
Stored as references, not values:
-
Object: Key-value pairs
javascript{ name: "John", age: 25 } -
Array: Ordered list
javascript[1, 2, 3, 4, 5] -
Function: Callable code block
javascriptfunction greet() { return "Hello"; }
Type Checking
Use typeof operator to check types:
javascripttypeof 42; // "number" typeof "text"; // "string" typeof true; // "boolean" typeof undefined; // "undefined" typeof null; // "object" (bug!) typeof {}; // "object" typeof []; // "object" typeof function(){}; // "function"
Important Notes:
- JavaScript is dynamically typed
- Variables can change types
- Use
===for strict equality checks typeof nullreturns"object"(historical bug)