Sobes.tech
Middle

JavaScript task: determine the type (typeof) for various expressions — new Array(), object, Map, number, Infinity, result of concatenating strings and numbers.

sobes.tech AI

Answer from AI

In JavaScript, the typeof operator returns a string indicating the type of the operand. Consider the following expressions:

console.log(typeof new Array());       // "object" — arrays in JS are objects
console.log(typeof {});                 // "object" — a regular object
console.log(typeof new Map());          // "object" — Map is also an object
console.log(typeof 42);                  // "number" — a number
console.log(typeof Infinity);            // "number" — infinity is also a number
console.log(typeof ("hello" + 5));    // "string" — concatenation results in a string

Explanation:

  • new Array() creates an array, which is an object.
  • {} is an object literal.
  • new Map() is a Map object.
  • Numbers and Infinity have the type "number".
  • When concatenating a string and a number, the number is converted to a string, resulting in a string.