0
0
Javascriptprogramming~5 mins

Primitive data types in Javascript

Choose your learning style9 modes available
Introduction

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.

When you want to store a number like age or price.
When you want to store a short piece of text like a name or a city.
When you want to store true or false answers, like if a light is on or off.
When you want to store a single character or symbol.
When you want to store a special value like 'nothing' or 'unknown'.
Syntax
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).

Examples
Basic examples of number, string, and boolean types.
Javascript
let age = 30; // number
let name = "Alice"; // string
let isStudent = false; // boolean
undefined means a variable has no value yet; null means an intentional empty value.
Javascript
let unknownValue; // undefined
let emptyValue = null; // null
Symbol creates a unique identifier, useful for special keys.
Javascript
let uniqueId = Symbol('id');
BigInt stores very large integers beyond normal number limits.
Javascript
let bigNumber = 123456789012345678901234567890n;
Sample Program

This program shows all primitive data types in JavaScript with example values and prints them.

Javascript
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);
OutputSuccess
Important Notes

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.

Summary

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.