JavaScript typeof Operator Last Updated : 13 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The typeof operator in JavaScript is used to determine the data type of a value or variable. It returns a string indicating the type, such as "string", "number", "boolean", "object", etc. JavaScript console.log(typeof "Hello"); console.log(typeof 42); console.log(typeof true); console.log(typeof {}); console.log(typeof undefined); Outputstring number boolean object undefined Key Points:Always outputs the data type as a string.Can be used anywhere without imports or dependencies.Determines the type of literals, variables, and expressions.Special Cases:typeof null returns "object" (a known quirk).Functions return "function" instead of "object".Useful for checking types in dynamic or untyped environments.Syntax:typeof operandoperand: The value or variable to check.Real-World ExamplesExample 1: Validating User Input JavaScript function fun(input) { if (typeof input === "number") { console.log("Valid number provided!"); } else { console.log("Please provide a number."); } } fun(42); fun("Hi"); OutputValid number provided! Please provide a number. Example 2: Dynamic Property Access JavaScript const settings = { darkMode: true }; if (typeof settings.darkMode === "boolean") { console.log("Dark mode setting is valid."); } OutputDark mode setting is valid. Primitive Data TypesPrimitive data types in JavaScript are basic data types that represent single values. They include:Data TypeDescriptionNumberRepresents numeric values like integers and floating-point numbers.StringRepresents textual data enclosed within single quotes ('') or double quotes ("").BooleanRepresents true or false values.UndefinedRepresents a variable that has been declared but has not been assigned a value.NullRepresents the intentional absence of any object value.SymbolRepresents a unique and immutable data type used as the key of an object's property.BigIntRepresents large integers beyond the limit of the Number type.Limitationstypeof null returns "object", which is misleading and a legacy bug in JavaScript.Cannot distinguish between arrays and objects (typeof [] is "object").Cannot differentiate between classes or specific object types.typeof NaN returns "number", though it's technically "not a number." Comment More info GeeksforGeeks Improve Article Tags : JavaScript Web Technologies Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like