🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Data Types

AI & DATA SCIENCE // data-types

JavaScript has 8 data types: String, Number, BigInt, Boolean, Undefined, Null, Symbol, and Object.

Syntax

typeof 'hello'  // 'string'
typeof 42       // 'number'
typeof true     // 'boolean'
typeof undefined // 'undefined'
typeof null     // 'object' (historical bug)
typeof {}       // 'object'
typeof []       // 'object'

Deep Dive Course

JavaScript is **dynamically typed**: variables can hold any type and types can change at runtime. There are 7 **primitive** types (immutable) and 1 **reference** type (Object, including Arrays and Functions).

1Understanding Data Types

JavaScript is dynamically typed: variables can hold any type and types can change at runtime. There are 7 primitive types (immutable) and 1 reference type (Object, including Arrays and Functions).

💡

typeof null === 'object' is a legacy bug from JS v1. Use === null to check for null.

editor.html
// Primitive types
let str  = 'hello';       // string
let num  = 42;            // number
let big  = 9007199n;      // bigint
let bool = true;          // boolean
let undef;                // undefined
let nul  = null;          // null
console.log(typeof str, typeof num, typeof bool);
localhost:3000

2Practical Example

Here is a real-world application of Data Types showing how it is used in production JavaScript code.

editor.html
// Type coercion pitfall
console.log('5' + 3);   // '53'  (string concat)
console.log('5' - 3);   // 2     (numeric)
console.log('' == false); // true (loose equality)
console.log('' === false); // false (strict)
localhost:3000

3Best Practices

Follow these guidelines when working with Data Types:

1. Use typeof to check primitives

2. Use Array.isArray() for arrays

3. Prefer strict equality (===) to avoid type coercion

⚠️

Tip: typeof null === 'object' is a legacy bug from JS v1. Use === null to check for null.

editor.html
// Primitive types
let str  = 'hello';       // string
let num  = 42;            // number
let big  = 9007199n;      // bigint
let bool = true;          // boolean
let undef;                // undefined
let nul  = null;          // null
console.log(typeof str, typeof num, typeof bool);
localhost:3000

Examples

Example 01Basic Usage
// Primitive types
let str  = 'hello';       // string
let num  = 42;            // number
let big  = 9007199n;      // bigint
let bool = true;          // boolean
let undef;                // undefined
let nul  = null;          // null
console.log(typeof str, typeof num, typeof bool);
Example 02Advanced Example
// Type coercion pitfall
console.log('5' + 3);   // '53'  (string concat)
console.log('5' - 3);   // 2     (numeric)
console.log('' == false); // true (loose equality)
console.log('' === false); // false (strict)

Best Practices

  • Use typeof to check primitives
  • Use Array.isArray() for arrays
  • Prefer strict equality (===) to avoid type coercion

Interview Question

What is the difference between null and undefined?

Hint: One is intentional absence, the other is uninitialized.

undefined means a variable was declared but not assigned a value. null is an intentional assignment representing 'no value'. typeof undefined === 'undefined', typeof null === 'object'.

Exercises

MediumPractice using Data Types in a real scenario.
View Solution
// Primitive types
let str  = 'hello';       // string
let num  = 42;            // number
let big  = 9007199n;      // bigint
let bool = true;          // boolean
let undef;                // undefined
let nul  = null;          // null
console.log(typeof str, typeof num, typeof bool);

Frequently Asked Questions

What is the difference between null and undefined?

undefined means a variable was declared but not assigned a value. null is an intentional assignment representing 'no value'. typeof undefined === 'undefined', typeof null === 'object'.

Related Functions

VariablesConstantsOperators