Interesting Facts About JavaScript Data Types
JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It comes with its unique take on data types that sets it apart from languages like C, C++, Java, or Python. Understanding how JavaScript handles data types will be very interesting, which can help you write better, more efficient code.
Here are some interesting facts about JavaScript Data Types
Only One Number Type
Unlike languages like C++, Java, or Python, where numbers are categorized as int, float, or double, JavaScript has just one number type: a 64-bit floating-point number (IEEE 754 standard).
- This means all integers and decimals are treated the same.
- Integer precision is accurate up to 15 digits
console.log(999999999999999);
console.log(9999999999999999);
Output
999999999999999 10000000000000000
In C++ or Java, you must explicitly define the type (e.g., int or float), and exceeding the typeâs range results in an error. JavaScript silently switches to floating-point behaviour.
Dynamic Typing
JavaScript is dynamically typed, meaning a variable's type can change at runtime, unlike statically typed languages like Java or C++.
let data = 42; // Initially a number
data = "Hello"; // Now a string
data = true; // Now a boolean
Primitive vs. Reference Types
JavaScript divides its data types into primitive and reference types
- Primitive Types: Stored directly in memory and are immutable. Examples of primitive type are number, string, boolean, undefined, null, symbol, bigint
- Reference Types: Objects and arrays are stored as references.
let obj1 = { a: 1 };
let obj2 = obj1;
obj2.a = 2;
console.log(obj1.a);
Output
2
undefined and null Are Separate Types
JavaScript treats undefined and null as distinct types, which can confuse developers from other languages.
- undefined: Represents a variable that has been declared but not assigned a value.
- null: Represents an intentional absence of a value.
let a;
console.log(a);
let b = null;
console.log(b);
Output
undefined null
In many languages, null or None is the default for uninitialized variables. JavaScriptâs separate undefined type adds a layer of complexity.
typeof null Returns "object"
One unusual behaviour is typeof operator identifies null as an "object". This is a well-known bug from the early days of JavaScript but remains for backward compatibility.
console.log(typeof null);
Output
object
This behavior is peculiar to JavaScript and isnât seen in other languages.
Bitwise Operations and 32-Bit Conversion
In JavaScript, all numbers are stored as 64-bit floating-point numbers, as defined by the IEEE 754 standard. However, bitwise operations in JavaScript are performed on 32-bit signed integers. This involves a multi-step conversion process
- Conversion to 32-Bit Signed Integer: When a bitwise operation is executed, the 64-bit floating-point number is converted to a 32-bit signed binary number.
- Bitwise Operation: The operation (e.g., AND, OR, XOR, etc.) is performed using the 32-bit binary representation.
- Conversion Back to 64-Bit: The result is converted back to a 64-bit floating-point number.
let n = 5.5; // Stored as a 64-bit floating-point number
let res = n | 0; // Bitwise OR operation
console.log(res); // Output: 5
Output
5
The && Operator Returns Actual Values
Unlike many other languages where the && (logical AND) operator strictly evaluates to a boolean (true or false), in JavaScript, it returns the actual value of the last operand evaluated. The behavior depends on whether the first operand is true or false
- If the First Operand is False: The operation short-circuits, and the first operand is returned without evaluating the second.
- If the First Operand is True: The second operand is evaluated and returned.
let x = 5;
let y = 0;
// 5 (true) && 0 (false)
let res = x && y;
console.log(res);
// 5 (true) && 10 (true)
res = x && 10;
console.log(res);
Output
0 10
NaN Is a Number
JavaScript includes NaN (Not-a-Number) as a value of type number.
console.log(typeof NaN);
console.log(NaN === NaN);
Output
number false
Most languages like Python or Java handle NaN differently and donât classify it as a number.
BigInt: Numbers Beyond 64-Bit Precision
To handle integers larger than 64 bits, JavaScript introduced the BigInt type in ES2020.
const bigN = 123456789012345678901234567890n;
console.log(bigN + 1n);
Output
123456789012345678901234567891n
While Python supports arbitrary precision integers by default, languages like Java or C++ require libraries to handle such large numbers.
Strings as Immutable Primitives
In JavaScript, strings are immutable primitives, even though they can be accessed like arrays.
let s = "Hello";
s[0] = "h"; // No effect
console.log(s);
Output
Hello
While string immutability exists in Java, JavaScriptâs syntax for accessing characters like arrays is unusual.
A character is also a string
There is no separate type for characters. A single character is also a string.
let s1 = "gfg"; // String
let s2 = 'g'; // Character
console.log(typeof s1);
console.log(typeof s2);
Output
string string
Symbol: Unique Identifiers
JavaScript introduces the symbol type for creating unique identifiers, a feature not commonly seen in other languages.
const sym1 = Symbol("id");
const sym2 = Symbol("id");
console.log(sym1 === sym2);
Output
false
Symbols ensure uniqueness, which is helpful for avoiding naming conflicts in object properties.
Everything Is an Object(Sort of):
In JavaScript, Functions are objects, arrays are objects, and even primitive values can behave like objects temporarily when you try to access properties on them.
let s = "hello";
console.log(s.length);
// Example with a number
let x = 42;
console.log(x.toString());
// Example with a boolean
let y = true;
console.log(y.toString());
/* Internal Working of primitives
to be treeated as objects
// Temporary wrapper object
let temp = new String("hello");
console.log(temp.length); // 5
// The wrapper is discarded after use
temp = null; */
Output
5 42 true
Falsy and Truthy Values
JavaScriptâs loose typing leads to unique rules for truthiness and falsiness:
- Falsy Values: false, 0, -0, "", null, undefined, NaN, document.all
- Truthy Values: Everything else.
if ("0") console.log("Truthy");
if (0) console.log("Falsy"); //no output
Output
Truthy
In this code, because 0 is false it will not execute the 2nd if statement as if statement runs only if condition is true.
This behavior doesnât exist in strongly-typed languages like C++ or Java.
The && Operator Returns Actual Values
Type Coercion and Dual Equality
JavaScript allows automatic type conversion during comparisons (==), which can yield surprising results.
console.log(5 == "5");
console.log(5 === "5");
Output
true false
Most languages enforce strict type equality for comparisons.
Arrays Are Objects
JavaScript arrays are technically objects, which allows them to have non-integer keys.
const a = [1, 2, 3];
a["key"] = "value";
console.log(a.key);
Output
value
In most languages, arrays are strictly defined structures and donât allow such behavior.
Dynamic Object Properties
JavaScript objects can have properties added, modified, or removed at runtime.
const obj = {};
obj.newProperty = "Hello";
console.log(obj.newProperty); // Output: Hello
Output
Hello
Unlike Java or C++, you donât need to predefine object structure in JavaScript.