menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

The Mechan...
source image

Medium

1M

read

69

img
dot

Image Credit: Medium

The Mechanics Behind JavaScript’s typeof and instanceof

  • typeof in JavaScript is a method to determine the type of a given value, but it has some quirks like returning 'object' for null and arrays.
  • It categorizes values into only eight possible results, such as 'number', 'boolean', and 'function'.
  • Checking for null with typeof can be misleading, and it's advised to use strict equality checks for null values.
  • typeof considers arrays as objects in JavaScript because arrays are a special type of object with additional behaviors.
  • To specifically check if a value is an array, Array.isArray() should be used.
  • Functions are treated uniquely by typeof, returning 'function' as the type, even though functions are technically objects.
  • Instances of functions like async generator functions or callable Proxy objects are also categorized as 'function' by typeof.
  • For primitive types, typeof works as expected, providing a straightforward way to identify basic values.
  • Boxed versions of primitives, created by wrapper constructors, may lead to confusion as typeof returns 'object' for them.
  • The instanceof operator in JavaScript examines the connection between an object and its prototype chain, focusing on inheritance rather than creation.

Read Full Article

like

4 Likes

For uninterrupted reading, download the app