JavaScript typeof Operator & Results list

typeof operator in Javascript; Javascript interview; web developer interview
Share this:

typeof operator returns a string as a result giving the type of an operand.

Syntax:

typeof operand
or,
typeof(operand)


NUMBER:

#Line number 5:

Math.LN4 mean natural log of 4 whose output is
Math.LN4 =ln(4) which is nearly equal to 1.3863


#Line number 7 :

This is one of the odd nature of typeof which output ‘number‘ string though it’s a Not a Number. This idiosyncrasy will be picked in many interviews and need to be in mind for proper coding practice.

#Line number 8 and 9 :

Number() function tries to parse the string to a number so line number 8 gives ‘number’ as output. In addition, line number 9 also gives a number.

This is because Number() tries to coerce the string inside which is ‘shoe’. Number(‘shoe’) return NaN and from line number 7, we know that typeof NaN is ‘number’.


STRING :

#in line number 4, we can see ‘1’ is also a string because a number within a string is string type.
#In line number 5, typeof 1 operator always return string ie ‘number’ . So typeof ‘number’ is ‘string’
and,
#In line number 6, String(1) function convert integer 1 into a string.


OBJECT :

#Line n. 1 – An array is a general object in Javascript. There are just six data types in JavaScript – number, string, null, Boolean, undefined and object.

#Line n. 3: – This new Date( ) is a constructor that creates a new date object with the current date and time.

#Line n. 4: – In javascript regular expression is also an object. In that code regex is any regex expression.

#Line n. 5-8: In JS, new operators create a new object which is inherited from the contractor’s prototype so typeof returns ‘object’.

#Line n. 9: This is another idiosyncrasy of Javascript that null is itself primitive datatype but typeof null is ‘object’.This is regarded as a bug in JS so checking for null cannot be performed by typeof operator .

FUNCTION :

typeof is asked in an interview most of the time. It is required for a programmer to know about this operator so this is a bank of the typeof operator that I wanted to create that you can come and check back when needed.

Thank you for reading and your time.

Happy Learning, Happy Coding.

Stay Safe and Take Care. Help others in need.

See you soon !!!

1

Related posts

Leave a Reply