Primitive data types are the basic building blocks to store simple values in a program. They help us keep and use small pieces of information like numbers or words.
Primitive data types in Javascript
let numberExample = 42; let stringExample = "hello"; let booleanExample = true; let undefinedExample; let nullExample = null; let symbolExample = Symbol('id'); let bigintExample = 9007199254740991n;
JavaScript has 7 primitive data types: number, string, boolean, undefined, null, symbol, and bigint.
Primitive values are stored directly and are immutable (cannot be changed).
let age = 30; // number let name = "Alice"; // string let isStudent = false; // boolean
let unknownValue; // undefined let emptyValue = null; // null
let uniqueId = Symbol('id');
let bigNumber = 123456789012345678901234567890n;
This program shows all primitive data types in JavaScript with example values and prints them.
console.log("Primitive Data Types Examples:"); let numberExample = 100; let stringExample = "Hello World"; let booleanExample = true; let undefinedExample; let nullExample = null; let symbolExample = Symbol('unique'); let bigintExample = 9007199254740991n; console.log("Number:", numberExample); console.log("String:", stringExample); console.log("Boolean:", booleanExample); console.log("Undefined:", undefinedExample); console.log("Null:", nullExample); console.log("Symbol:", symbolExample.toString()); console.log("BigInt:", bigintExample);
Time complexity: Accessing primitive values is very fast, constant time O(1).
Space complexity: Each primitive uses a small fixed amount of memory.
Common mistake: Confusing null and undefined; undefined means no value assigned, null means empty value assigned.
Use primitives for simple data. For collections or complex data, use objects or arrays.
Primitive data types store simple, single values like numbers or text.
JavaScript has 7 primitive types: number, string, boolean, undefined, null, symbol, and bigint.
Primitives are fast and use little memory, perfect for basic data storage.
Practice
Solution
Step 1: Recall JavaScript primitive types
JavaScript primitive types include string, number, boolean, undefined, null, symbol, and bigint.Step 2: Identify non-primitive types
Object is not a primitive type; it is a complex data type used to store collections.Final Answer:
object -> Option AQuick Check:
Primitive types exclude objects [OK]
- Confusing object with primitive types
- Thinking null is an object
- Mixing symbol with object
Solution
Step 1: Check string declaration syntax
Strings must be enclosed in matching quotes: single ('') or double ("") or backticks (``).Step 2: Validate each option
let name = 'John'; uses single quotes correctly. let name = John; lacks quotes. Options C and D have unclosed quotes.Final Answer:
let name = 'John'; -> Option BQuick Check:
Strings need matching quotes [OK]
- Missing quotes around string
- Using mismatched quotes
- Forgetting semicolon (optional but good)
console.log(typeof 123); console.log(typeof 'hello'); console.log(typeof true);
Solution
Step 1: Understand typeof operator
typeof returns the type of a value as a lowercase string like "number", "string", or "boolean".Step 2: Check each console.log output
typeof 123 is "number", typeof 'hello' is "string", typeof true is "boolean".Final Answer:
"number", "string", "boolean" -> Option AQuick Check:
typeof returns lowercase type names [OK]
- Capitalizing type names
- Using short forms like 'num'
- Confusing typeof output with values
let value = null; console.log(typeof value);
Solution
Step 1: Understand typeof null behavior
typeof null returns "object" due to a historical JavaScript bug.Step 2: Identify confusion
This is confusing because null is a primitive but typeof shows "object".Final Answer:
typeof null returns 'object' which is confusing -> Option CQuick Check:
typeof null is 'object' (quirk) [OK]
- Expecting 'null' from typeof
- Thinking null is an object
- Assuming code throws error
Solution
Step 1: Understand number limits in JavaScript
The number type can safely represent integers only up to 2^53 - 1.Step 2: Identify type for large integers
bigint is designed to store integers larger than the safe limit without losing precision.Final Answer:
bigint -> Option DQuick Check:
Use bigint for very large integers [OK]
- Using number for very large integers
- Using string instead of bigint
- Confusing symbol with number types
Ready to prove it?
Pass the test and this pattern moves unknown
