A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-typeof-operator/ below:

JavaScript typeof Operator - GeeksforGeeks

JavaScript typeof Operator

Last Updated : 13 Dec, 2024

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); 

Output
string
number
boolean
object
undefined
Key Points: Syntax:
typeof operand
Real-World Examples Example 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"); 

Output
Valid 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.");
}

Output
Dark mode setting is valid.
Primitive Data Types

Primitive data types in JavaScript are basic data types that represent single values. They include:

Data Type Description Number Represents numeric values like integers and floating-point numbers. String Represents textual data enclosed within single quotes ('') or double quotes (""). Boolean Represents true or false values. Undefined Represents a variable that has been declared but has not been assigned a value. Null Represents the intentional absence of any object value. Symbol Represents a unique and immutable data type used as the key of an object's property. BigInt Represents large integers beyond the limit of the Number type. Limitations

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4