Sobes.tech
Junior — Middle

What value does the typeof operator return in JavaScript?

sobes.tech AI

Answer from AI

In JavaScript, the typeof operator returns a string indicating the type of the operand passed. For example:

  • typeof 42 returns 'number'
  • typeof 'hello' returns 'string'
  • typeof true returns 'boolean'
  • typeof undefined returns 'undefined'
  • typeof null returns 'object' (language peculiarity)
  • typeof {} returns 'object'
  • typeof function() {} returns 'function'

Example:

console.log(typeof 123); // 'number'
console.log(typeof 'abc'); // 'string'
console.log(typeof {}); // 'object'
console.log(typeof null); // 'object'
console.log(typeof undefined); // 'undefined'
console.log(typeof function() {}); // 'function'

Thus, typeof helps determine the basic type of a value in JavaScript.