How to determine the type of value in JavaScript?


JavaScript has some built-in types, including numbers, strings, booleans, and objects.

usetypeofOperator, we can check what the type of the value assigned to the variable is.

E.g:

typeof 'test'

Note that it is not a function, but an operator, so parentheses are not required.

Using it, we will return a string with one of the following values:

  • 'number'
  • 'string'
  • 'boolean'
  • 'undefined'
  • 'bigint'
  • 'symbol'
  • 'object'
  • 'function'

Please note that there is nonullType and

typeof null

Will return'object'.

Array will return'object'and also:

typeof [1, 2, 3] //'object'

A function is a special kind of object, because we can add properties and methods to the function:

const talk = () => {}
talk.test = true

But they have their own value'function'If we usetypeofoperator.

Download mine for freeJavaScript beginner's manual


More js tutorials: