How does the typeof operator work in JavaScript

In JavaScript, the typeof operator is used to determine the type of a variable or an expression. It returns a string indicating the data type of the operand.

Basic Usage:

            typeof variableName;

  1. Primitive Types:
    • For primitive types (except null), typeof returns a string indicating the type:
                              typeof 42; // "number"
                              typeof "Hello"; // "string"
                              typeof true; // "boolean"       
                              typeof undefined; // "undefined"
  2. Objects:
    • For objects, except null, typeof returns "object":
                              typeof {}; // "object"
                              typeof []; // "object"
                              typeof new Date(); // "object"                        
  3. Function:
    • typeof returns "function" for functions:
                              typeof function() {}; // "function"
  4. null:
    • typeof null returns "object", which is a historical quirk in JavaScript:
                              typeof null; // "object"
  5. Undefined Variables:
    • For undefined variables, typeof returns "undefined":
                              let x;
                              typeof x; // "undefined"                        
  • typeof is a unary operator, and it does not require parentheses when used.
  • It's important to note that typeof has some quirks, like returning "object" for null . This behavior is a historical mistake in the language and has been kept for backward compatibility reasons.
  • typeof is useful for performing type checks or determining the type of a variable before performing certain operations.

            function example(value) {
                if (typeof value === 'number') {
                  return 'It is a number';
                } else if (typeof value === 'string') {
                  return 'It is a string';
                } else {
                  return 'It is of another type';
              console.log(example(42)); // Outputs: 'It is a number'
              console.log(example('Hello')); // Outputs: 'It is a string'
              console.log(example(true)); // Outputs: 'It is of another type'              

typeof is a handy tool for performing basic type checks, but for more comprehensive type checking or handling different object types, other methods or libraries might be neede

How To Open a Port on Linux

Opening a port on Linux involves configuring the firewall to allow traffic through the specified port. Here's a step-by-step guide to achieve this, assuming you are using ufw (Uncomplicated Firewall) or iptables for managing your firewall settings. u …

read more

Troubleshooting Latency Issues on App Platform

Troubleshooting latency issues on an app platform can be complex, involving multiple potential causes across the network, server, application code, and database. Here’s a structured approach to identifying and resolving latency issues. Identify …

read more