Data Types (String, Number, Boolean, null, undefined, Symbol, BigInt)

Expert-Level Explanation

JavaScript provides several data types to hold different kinds of values. Here are the main types:

  • String: represents textual data, e.g.,"Hello, World!".

  • Number: represents numerical values, e.g., 42 or 3.14.

  • Boolean: represents logical entities and can be either true or false.

  • null: represents the intentional absence of any object value. It's often used to indicate a "nothing" or "empty" value.

  • undefined: Indicates a variable that has been declared but has not yet been assigned a value.

  • Symbol: Introduced in ES6, it represents a unique value that's not equal to any other value.

  • BigInt: Also a newer addition, it allows the representation of integers larger than 2^53 - 1, which is the largest number JavaScript can reliably represent with the Number type.

Creative Explanation

Think of JavaScript data types as different forms of communication:

  • String: Like sending a text message, it conveys words or sentences.

  • Number: Similar to using numbers or calculations, like when you count apples or measure distance.

  • Boolean: A simple yes (true) or no (false) decision, like answering a yes-or-no question.

  • null: sending an empty box, deliberately showing it's empty.

  • undefined: Like having an empty speech bubble, something is supposed to be there, but it's not yet defined.

  • Symbol: creating a unique, one-of-a-kind badge or emblem that no one else has.

  • BigInt: writing a super long number, like the number of stars in the universe, which can't be contained in a regular notebook.

Practical Explanation with Code

let stringValue = "Hello, World!"; // String
let numberValue = 123;            // Number
let booleanValue = true;          // Boolean
let nullValue = null;             // null
let undefinedValue;               // undefined, no value assigned
let symbolValue = Symbol("unique"); // Symbol, unique
let bigIntValue = 123456789012345678901234567890n; // BigInt

Real-world Example

  • String: Like a name tag, it identifies someone or something with text.

  • Number: Similar to counting money or calculating a bill.

  • Boolean: Like a light switch, it can only be on (true) or off (false).

  • null: an empty parking space, specifically marked but with no car in it.

  • undefined: A form with a blank field, awaiting information.

  • Symbol: A unique fingerprint, different for each individual.

  • BigInt: Like the odometer of a spacecraft, it counts very long distances that a regular car's odometer can't handle.


Difference Between null and undefined

Expert Answer

In JavaScript, null and undefined both represent the absence of value, but in different ways:

  • nullis an assignment value. It indicates that a variable has been explicitly set to "no value."

  • undefinedmeans a variable has been declared but not yet assigned any value. It's the default state of a variable.

Creative Explanation

Imagine null as an empty box that someone has intentionally given to you, indicating they've given you nothing. On the other hand, undefined is like an empty spot where a box should be; nobody has given you a box yet.

Practical Explanation with Code

let a = null; // Explicitly setting a to 'no value'
let b; // Just declaring b, not assigning any value
console.log(a); // null
console.log(b); // undefined

Real-world Example

If null is an empty parcel deliberately sent to you, undefined is having no parcel at all.

Data TypeDescriptionExample Values
NumberNumeric data (integers and floats)123, 3.14, -500
StringSequence of characters for text data"Hello", 'World'
BooleanLogical data type (true or false)true, false
UndefinedRepresents undefined or unassigned valueundefined
NullRepresents intentional absence of any object value, Type of Null is objectnull
ObjectComplex data structures like arrays, dates, literals, etc.{ name: 'Alice' }, [1, 2, 3]
SymbolA unique and immutable primitive introduced in ES6Symbol('id')
BigIntRepresents integers with arbitrary precision (ES11/ES2020)9007199254740991n

Did you find this article valuable?

Support Akash Thoriya by becoming a sponsor. Any amount is appreciated!