Typeof in JavaScript
The typeof is an operator that returns the data type of a given value or variable. It returns a string that specifies the type of the value or variable.
Here are the different data types that can be returned by typeof:
"undefined": if the value is undefined"boolean": if the value is a boolean (true or false)"number": if the value is a number"bigint": if the value is a bigint"string": if the value is a string"symbol": if the value is a symbol"object": if the value is an object, a function, or null (null is considered an object in JavaScript)"function": if the value is a function
Here are a few examples of using typeof:
typeof undefined; // "undefined"
typeof true; // "boolean"
typeof 42; // "number"
typeof 9007199254740991n; // "bigint"
typeof "hello"; // "string"
typeof Symbol("foo"); // "symbol"
typeof {name: "Alice", age: 25}; // "object"
typeof function () {}; // "function"
typeof null; // "object"
The Data Type of typeof
The typeof operator always returns a string, which represents the data type of the value or variable passed as an argument. This means that the data type of typeof itself is a string.
Here's an example to illustrate this:
typeof typeof 42; // "string"
In this example:
- The inner
typeof 42expression returns the string "number". - The outer
typeofoperator returns the string"string". - Which is the data type of the result of the inner expression.
So, the data type of typeof is always a string, regardless of the argument passed to it.
The constructor Property
The constructor property is a property of objects that refers to the constructor function that created the object. Every object in JavaScript has a constructor property, except for null and undefined.
As an example:
function Person(name) {
this.name = name;
}
const person1 = new Person("Alice");
console.log(person1.constructor); // returns "Person"
const person2 = new person1.constructor("Bob");
console.log(person2.name); // returns "Bob"
In this example:
- The
Personfunction is a constructor function that creates objects with a name property. - The
person1object is created using the new keyword and thePersonconstructor function, and therefore its constructor property refers to thePersonfunction.
Next:
- We can also create a new object
person2using the constructor property ofperson1 - Which returns the same constructor function that created
person1. - We then create a new object
person2using this constructor function and passing in the name "Bob". - The resulting object
person2also has a name property, which we can access withperson2.name.
JavaScript undefined
The undefined is a primitive value that is automatically assigned to a variable or property when it is declared but not initialized with a value. It is also the default return value of a function that does not explicitly return a value.
As an example:
let x; // x is undefined
function foo() {}
console.log(foo()); // logs undefined
const obj = {};
console.log(obj.property); // logs undefined
In the first example:
- The variable
xis declared but not initialized, so its value isundefined. - In the second example, the function
foodoes not have areturnstatement, so it implicitly returnsundefined. - In the third example, the property property of the object obj does not exist, so accessing it returns
undefined.
The undefined is a primitive value, not an object, so it does not have any methods or properties. However, it is still a value, and can be compared with other values using the === operator.
JavaScript null
The null is a primitive value that represents an intentional absence of any object value. It is often used to indicate that a variable or property does not have a value, or that a function intentionally returns no value.
As an example:
let x = null; // x has no value
function foo() {
return null;
}
const obj = {
property: null,
};
In the first example
- The variable
xis explicitly assigned the valuenull, indicating that it does not have a value. - In the second example, the function foo intentionally returns the value
null, indicating that it has no return value. - In the third example, the property
propertyof the objectobjis explicitly assigned the valuenull, indicating that it does not have a value.
Note that null is a primitive value, not an object, so it does not have any methods or properties. Also note that null is distinct from undefined, which represents a value that is not yet defined or has not been assigned a value.
Empty Values
In JavaScript, there are two values that represent an "empty" or "no value" state: null and undefined.
null is the intentional absence of any object value, while undefined is a value that is not yet defined or has not been assigned a value. Both are falsy values, which means they are considered false in a boolean context.
In addition to null and undefined, there are also empty string (""), zero (0), and NaN, which can be considered as "empty" values in certain contexts.
Here are some examples of empty values in JavaScript:
let x; // undefined
let y = null; // null
let z = ""; // empty string
let n = 0; // zero
let a = NaN; // not a number
Note that null and undefined are distinct values with different meanings, and are not interchangeable. In general, undefined is used to represent a value that is not yet defined or has not been assigned, whereas null is used to represent an intentional absence of any object value.