typeof operator in Javascript

ยท

4 min read

typeof operator in Javascript

Do you actually know how to use typeof in Javascript?
Do you now what are typeof operator capabilities?

Well, we are going to explore typeof in depth.

๐Ÿ’ก
Do you know typeof is not specific to Typescript. It was already an operator in Javascript.

typeof in Javascript

typeof is an operator which can be used to get the type of any operand. It will return a string which denotes the type. It can also be used in runtime to check the type.

It will return possible value from string, number, boolean, object, function, undefined, symbol and bigint

What typeof returns based on the value

Let start with an example. Let say you have a value and you want to know its type. Doesn't matter where this value(in this case myName and myAge which holds a string and number respectively ) comes from. It may come from an API or from an external library. Just for simplicity we are taking ourself into a variable.

let myName = "Sam";
let myNameType = typeof myName;
console.log(myNameType); // "string"

let myAge = 18;
let myAgeType = typeof myAge;
console.log(myAgeType); // "number"

It will return a string in case of myName and number in case of myAge.

Javascript will check the typeof value that you have provided and returns its type as:

let's create a Pseudocode for getting typeof value:

1. Let val be the result of evaluating UnaryExpression.
2. If val is a Reference Record, then
    a. If IsUnresolvableReference(val) is true, return "undefined".
3. Set val to the result of GetValue(val).
4. If val is undefined, return "undefined".
5. If val is null, return "object".
6. If val is a String, return "string".
7. If val is a Symbol, return "symbol".
8. If val is a Boolean, return "boolean".
9. If val is a Number, return "number".
10. If val is a BigInt, return "bigint".
11. Assert: val is an Object.
12. NOTE: This step is replaced in section B.3.6.3.
13. If val has a [[Call]] internal slot, return "function".
14. Return "object".

Brief Explanation of Pseudocode

Unary Expression Evaluation Process

  1. Evaluate Unary Expression:

    • Let val be the result of evaluating UnaryExpression.

    • This step involves evaluating the expression provided in UnaryExpression and storing the result in the variable val.

  2. Check if val is a Reference:

    • In JavaScript, a reference is a value that points to a memory location where the actual value is stored. If the reference cannot be resolved (i.e., the variable is not defined), it returns "undefined".

    • Example: If UnaryExpression is a variable name that hasn't been declared, val would be a reference that cannot be resolved.

  3. Get the Actual Value of val:

    • This step retrieves the actual value from the reference or from the evaluated expression.

    • Example: If UnaryExpression is "true", val would be true.

  4. Check for Undefined Value:

    • If the value of val is undefined, the function returns "undefined".

    • Example: If UnaryExpression is "undefined", val would be undefined.

  5. Check for Null Value:

    • In JavaScript, null is an object that represents the absence of any value.

    • Example: If UnaryExpression is "null", val would be null.

  6. Check for String Value:

    • If the value of val is a string, the function returns "string".

    • Example: If UnaryExpression is "'hello'", val would be "hello".

  7. Check for Symbol Value:

    • Symbols are a new primitive type introduced in ECMAScript 2015.

    • Example: If UnaryExpression is "Symbol('example')", val would be a symbol.

  8. Check for Boolean Value:

    • If the value of val is a boolean, the function returns "boolean".

    • Example: If UnaryExpression is "true", val would be true.

  9. Check for Number Value:

    • If the value of val is a number, the function returns "number".

    • Example: If UnaryExpression is "42", val would be 42.

  10. Check for BigInt Value:

    • BigInt is a built-in object that provides a way to represent whole numbers larger than 2(power 53) - 1.

    • Example: If UnaryExpression is "BigInt(9007199254740991)", val would be a BigInt.

  11. Check for Function Object:

    • If the value of val is an object and it can be invoked as a function, the function returns "function".

    • Example: If UnaryExpression is "function() { return 42; }", val would be a function.

  12. Return "object" Otherwise:

    • If none of the above conditions match, the function returns "object".

I hope you learn from this article. See you next time ๐Ÿ™‚

Did you find this article valuable?

Support decpk by becoming a sponsor. Any amount is appreciated!

ย