Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Js Typeof in JavaScript

Js Typeof in JavaScript

typeof Operator in JavaScript

The typeof operator is used to determine the type of a JavaScript variable or expression. It returns a string indicating the type.


Syntax

typeof operand
  • operand can be a variable, literal, or expression.


Possible Return Values of typeof

TypeReturned StringExample
Number"number"typeof 42 ? "number"
String"string"typeof "hello" ? "string"
Boolean"boolean"typeof true ? "boolean"
Undefined"undefined"typeof undefined ? "undefined"
Object"object"typeof {} ? "object"
Function"function"typeof function() {} ? "function"
Symbol"symbol"typeof Symbol() ? "symbol"
BigInt"bigint"typeof 123n ? "bigint"
Null*"object"typeof null ? "object" (quirk)

Note: typeof null returning "object" is a known JavaScript quirk.


Examples

console.log(typeof 123);           // "number"console.log(typeof "JavaScript");  // "string"console.log(typeof true);          // "boolean"console.log(typeof undefined);     // "undefined"console.log(typeof {});            // "object"console.log(typeof null);          // "object" (special case)console.log(typeof function(){}); // "function"console.log(typeof Symbol("id")); // "symbol"console.log(typeof 10n);           // "bigint"

Using typeof in Conditions

let value = 42;if (typeof value === "number") {  console.log("It's a number!");} else {  console.log("Not a number");}

If you want, I can show you how to reliably check for null or arrays beyond just typeof!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql