JavaScript primitive data types
Primitive data types in JavaScript are the basic value types that are not objects. A primitive value represents a single value such as a text value, numeric value, boolean value, missing value, or unique symbol. JavaScript also has objects, but objects are non-primitive values and are handled differently in memory and comparison.
Modern JavaScript has seven primitive data types:
- string
- number
- bigint
- boolean
- null
- undefined
- symbol
A common beginner mistake is to say that primitive values are objects. Primitive values are not objects. However, JavaScript can temporarily wrap some primitive values with object wrappers so that you can use helpful methods and properties, such as 'hello'.toUpperCase() or 'hello'.length.
Seven primitive data types in JavaScript with examples
| Primitive data type | Example value | Used for | typeof result |
|---|---|---|---|
| string | 'Hello' | Text values | 'string' |
| number | 25, 3.14 | Integer and floating-point numeric values | 'number' |
| bigint | 9007199254740993n | Very large whole numbers | 'bigint' |
| boolean | true, false | Logical yes/no conditions | 'boolean' |
| undefined | undefined | A variable declared but not assigned a value | 'undefined' |
| null | null | Intentional absence of a value | 'object' |
| symbol | Symbol('id') | Unique identifiers | 'symbol' |
The result of typeof null is 'object'. This is a long-standing JavaScript behavior, but null is still a primitive value.
Primitive data types and wrapper objects in JavaScript
Primitive data types do not store methods inside the primitive value itself. JavaScript provides wrapper objects for some primitive data types so that methods and properties can be accessed when needed.
| Primitive Datatype | Wrapper Object |
| string | String |
| number | Number |
| boolean | Boolean |
| bigint | BigInt |
| symbol | Symbol |
For the primitive datatypes : null, undefined : there are no Wrapper Objects.
Although wrapper objects exist, it is usually better to use primitive values directly instead of creating wrapper objects with constructors such as new String(), new Number(), or new Boolean(). Wrapper objects can behave differently from primitive values during comparison and truthy/falsy checks.
Temporary wrapper behavior for primitive string values
const message = 'JavaScript';
console.log(message.length); // 10
console.log(message.toUpperCase()); // JAVASCRIPT
In this example, message is still a primitive string. JavaScript temporarily makes string methods and properties available while the expression is evaluated.
Initialize variables with primitive values
JavaScript is a dynamic language.
- Variables are not declared with a datatype.
- A variable is not fixed to a single datatype.
- A variable’s datatype changes with the value assigned to a variable.
- To know a datatype of value that is stored in a variable, use typeof variableName .
// primitive datatypes
var a = 1; // number
var b = 'Hello'; // string
var c = true; // boolean
var d; // undefined
var e = Symbol(54); // symbol
Note: Null datatype has only one value and that is null. In JavaScript a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.
Checking JavaScript primitive data types with typeof
The typeof operator is commonly used to check the data type of a primitive value. It returns a string such as 'string', 'number', 'boolean', 'undefined', 'symbol', or 'bigint'.
console.log(typeof 'Hello'); // string
console.log(typeof 25); // number
console.log(typeof 25n); // bigint
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof Symbol('id')); // symbol
console.log(typeof null); // object
Use value === null when you specifically need to check whether a value is null.
let selectedUser = null;
if (selectedUser === null) {
console.log('No user is selected.');
}
Primitive values are immutable in JavaScript
A primitive value itself cannot be changed. When you appear to change a primitive value, JavaScript creates or assigns a new primitive value. This is called immutability.
let word = 'cat';
let upperWord = word.toUpperCase();
console.log(word); // cat
console.log(upperWord); // CAT
The original string 'cat' is not modified. The method returns a new string value.
Primitive values compared with object values
Primitive values are compared by value. Objects are compared by reference. This difference is important when you compare strings, numbers, arrays, objects, dates, and wrapper objects.
console.log('hello' === 'hello'); // true
console.log(10 === 10); // true
console.log({} === {}); // false
console.log([] === []); // false
The two object literals look identical, but they are two different object references. The primitive values are equal because their actual values are the same.
Number and BigInt primitive data types in JavaScript
The number type is used for normal numeric values, including integers and decimal values. The bigint type is used for whole numbers that may be larger than the safe integer range of the number type.
const price = 99.5; // number
const count = 42; // number
const largeId = 9007199254740993n; // bigint
Do not mix number and bigint directly in arithmetic operations. Convert values deliberately when needed.
const normalNumber = 10;
const bigNumber = 20n;
// Convert explicitly before arithmetic
console.log(BigInt(normalNumber) + bigNumber); // 30n
null and undefined primitive values in JavaScript
undefined usually means a value has not been assigned. null usually means the absence of a value has been assigned intentionally.
let city;
console.log(city); // undefined
let selectedProduct = null;
console.log(selectedProduct); // null
In practice, use undefined for values JavaScript has not assigned yet, and use null when your program needs to clearly say that no value is currently available.
Common mistakes with JavaScript primitive data types
- Forgetting BigInt: Older tutorials may list six primitive types, but modern JavaScript includes
bigintas a primitive type. - Treating typeof null as proof that null is an object:
typeof nullreturns'object', butnullis a primitive value. - Creating wrapper objects unnecessarily: Prefer
'text',25, andtrueovernew String('text'),new Number(25), andnew Boolean(true). - Comparing objects like primitives: Primitive values compare by value, while arrays and objects compare by reference.
- Mixing number and bigint in arithmetic: Convert explicitly before arithmetic when both types are involved.
JavaScript primitive data types FAQs
What are the primitive data types in JavaScript?
The primitive data types in JavaScript are string, number, bigint, boolean, undefined, null, and symbol.
Is null a primitive data type in JavaScript?
Yes. null is a primitive value in JavaScript. The expression typeof null returns 'object', but that result is a special historical behavior and does not make null an object.
What is the difference between primitive and non-primitive data types in JavaScript?
Primitive values store simple values and are compared by value. Non-primitive values such as objects, arrays, and functions are objects and are compared by reference.
Do primitive data types have methods in JavaScript?
Primitive values do not store methods inside themselves. JavaScript temporarily uses wrapper objects for some primitive values, which is why expressions such as 'hello'.toUpperCase() work.
Why should wrapper objects be avoided for primitives?
Wrapper objects such as new String() and new Boolean() create objects instead of primitive values. This can cause confusing comparison and truthy/falsy behavior, so primitive literals are usually preferred.
Conclusion on JavaScript primitive data types
In this JavaScript Tutorial – Primitive Datatypes, we have learnt about the primitive data types available in JavaScript, how to initialize variables with primitive values, how typeof works, and how primitive values differ from objects. Remember that primitives are simple, immutable values, while objects are reference-based values.
Editorial QA checklist for JavaScript primitive data types
- Confirm that the tutorial lists all seven modern JavaScript primitive data types, including
bigint. - Check that
nullis described as a primitive value while also explaining thetypeof nullresult. - Verify that examples distinguish primitive value comparison from object reference comparison.
- Ensure wrapper objects are explained without encouraging unnecessary use of
new String(),new Number(), ornew Boolean(). - Review the code blocks for correct JavaScript syntax and PrismJS-compatible language classes.
TutorialKart.com