JavaScript is a dynamically typed language, meaning that data types do not need to be specified when declaring variables; instead, data types are automatically determined at runtime. JavaScript data types are primarily divided into two categories: primitive types and object types.
Primitive Types
- undefined: Represents an uninitialized variable, i.e., one that has been declared but not assigned a value.
- null: Represents a null value.
- boolean: Boolean type, with two values:
trueandfalse. - string: Represents textual data, such as "Hello, World!".
- number: Can be integers or floating-point numbers, such as
42or3.14159. - BigInt: Represents integers greater than 2^53 - 1.
- Symbol: Represents a unique, immutable data value.
Object Types
- Object: JavaScript objects are collections of key-value pairs. Almost all JavaScript values are object types, including arrays, functions, and other built-in objects.
Methods for Type Detection
In JavaScript, common methods for detecting variable types include:
typeofoperator: Used to detect the type of a variable. It is highly effective for primitive types but has limitations when dealing with object types andnull.
javascriptlet num = 42; console.log(typeof num); // "number" let str = "Hello"; console.log(typeof str); // "string" let flag = true; console.log(typeof flag); // "boolean" let bigIntNumber = 1234567890123456789012345678901234567890n; console.log(typeof bigIntNumber); // "bigint" let sym = Symbol('foo'); console.log(typeof sym); // "symbol" let und; console.log(typeof und); // "undefined" let obj = { key: 'value' }; console.log(typeof obj); // "object" let arr = [1, 2, 3]; console.log(typeof arr); // "object", despite being an array let func = function() {}; console.log(typeof func); // "function", functions are a special type of object let nul = null; console.log(typeof nul); // "object", a historical error
instanceofoperator: Used to determine if an object is an instance of another object.
javascriptlet arr = [1, 2, 3]; console.log(arr instanceof Array); // true console.log(arr instanceof Object); // true let d = new Date(); console.log(d instanceof Date); // true // Note: `instanceof` cannot detect primitive types
Array.isArray(): Used to determine if a value is an array.
javascriptlet arr = [1, 2, 3]; console.log(Array.isArray(arr)); // true
constructorproperty of objects: Can be used to determine the constructor of an object.
javascriptlet arr = [1, 2, 3]; console.log(arr.constructor === Array); // true let obj = {}; console.log(obj.constructor === Object); // true
Object.prototype.toString.call(): A general type detection method that accurately identifies various types of values.
javascriptlet d = new Date(); console.log(Object.prototype.toString.call(d)); // "[object Date]" let num = 42; console.log(Object.prototype.toString.call(num)); // "[object Number]" let str = "Hello"; console.log(Object.prototype.toString.call(str)); // "[object String]"
Note: When using type detection methods, choose the most appropriate one based on the specific context, as each method has its own use cases and limitations.