typeof運(yùn)算符用于確定變量或表達(dá)式的數(shù)據(jù)類(lèi)型,返回字符串如"number"、"string"、"object"等;其返回值包括"undefined"、"boolean"、"number"、"string"、"bigint"、"symbol"、"function"和"object";1. typeof null返回"object"是歷史遺留的bug;2. 要安全檢查null應(yīng)使用value === null;3. typeof適用于檢查變量是否定義、驗(yàn)證函數(shù)參數(shù)、調(diào)試等場(chǎng)景;4. 對(duì)數(shù)組和函數(shù)typeof返回"object"和"function",但無(wú)法準(zhǔn)確區(qū)分?jǐn)?shù)組與對(duì)象,需用Array.isArray();5. 使用typeof時(shí)需注意其局限性和邊緣情況。
The typeof
operator in JavaScript is used to determine the data type of a variable or expression. It returns a string indicating the type, such as "number"
, "string"
, or "object"
. While it seems straightforward, there are nuances that can trip people up if they're not aware.
What values does typeof
return?
There are only a handful of possible return values from typeof
. Here’s what you might see:
-
"undefined"
— for undeclared variables or variables that haven’t been assigned. -
"boolean"
— fortrue
orfalse
. -
"number"
— for any numeric value, includingNaN
. -
"string"
— for string values. -
"bigint"
— for large integers created with theBigInt
type. -
"symbol"
— for values created usingSymbol()
. -
"function"
— for functions (though technically functions are objects). -
"object"
— for objects, arrays, andnull
.
Wait—yes, typeof null
returns "object"
. That's one of those quirks we’ll cover next.
Why does typeof null
return "object"
?
This is a classic gotcha. If you run:
typeof null // returns "object"
You might expect "null"
as the result, but this behavior has been around since the early days of JavaScript. It’s basically a historical bug that’s now part of the language due to backward compatibility. So keep in mind: typeof null
gives "object"
— don’t rely on typeof
alone to check for null
.
To safely check for null
, use:
if (value === null) { ... }
That way, you avoid confusion between objects and actual null
values.
When is typeof
useful?
Despite its quirks, typeof
comes in handy in several scenarios:
Checking if a variable is defined:
if (typeof myVar === 'undefined') { ... }
Validating function parameters:
You can make sure a value passed into a function is of the expected type before doing operations on it.Debugging:
During development,typeof
helps confirm whether a value is what you expect — especially when dealing with dynamic inputs or third-party data.
However, if you need more precise type checking (like distinguishing arrays from objects), consider using Array.isArray()
or instanceof
.
What about functions and arrays?
When you use typeof
on a function, it returns "function"
:
typeof function() {} // "function"
That’s actually helpful because functions are technically objects in JavaScript, but typeof
treats them as a special case.
For arrays:
typeof [1, 2, 3] // "object"
So again, typeof
isn't enough to distinguish arrays from regular objects. Use Array.isArray()
for that.
Well, that’s how typeof
works — simple, occasionally surprising, but definitely useful once you know its limits. Just remember to double-check edge cases like null
and arrays.基本上就這些。
以上是操作員的類(lèi)型如何工作?的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門(mén)文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6
視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)

熱門(mén)話題

在c語(yǔ)言中,沒(méi)有開(kāi)根號(hào)運(yùn)算符,開(kāi)根號(hào)使用的是內(nèi)置函數(shù)“sqrt()”,使用語(yǔ)法“sqrt(數(shù)值x)”;例如“sqrt(4)”,就是對(duì)4進(jìn)行平方根運(yùn)算,結(jié)果為2。sqrt()是c語(yǔ)言?xún)?nèi)置的開(kāi)根號(hào)運(yùn)算函數(shù),其運(yùn)算結(jié)果是函數(shù)變量的算術(shù)平方根;該函數(shù)既不能運(yùn)算負(fù)數(shù)值,也不能輸出虛數(shù)結(jié)果。

對(duì)于Golang開(kāi)發(fā)者來(lái)說(shuō),“invaliduseof…operator”是一個(gè)常見(jiàn)的報(bào)錯(cuò)。這個(gè)報(bào)錯(cuò)通常會(huì)在使用變長(zhǎng)參數(shù)函數(shù)時(shí)出現(xiàn)。它在編譯時(shí)就會(huì)被檢測(cè)出來(lái),并指出哪些部分有問(wèn)題。這篇文章將介紹如何解決這個(gè)報(bào)錯(cuò)。一、什么是變長(zhǎng)參數(shù)函數(shù)變長(zhǎng)參數(shù)函數(shù)也被稱(chēng)為可變參數(shù)函數(shù),是Golang語(yǔ)言中的一種函數(shù)類(lèi)型。使用變長(zhǎng)參數(shù)函數(shù)可以像如下方式定義多個(gè)

在php中,“==”符號(hào)是一個(gè)比較運(yùn)算符,可以比較兩個(gè)操作數(shù)是否相等,語(yǔ)法“操作數(shù)1 == 操作數(shù)2”。“==”運(yùn)算符會(huì)比較、并測(cè)試左邊的變量(表達(dá)式或常量)是否與右邊的變量(表達(dá)式或常量)具有相同的值;它只比較變量的值,而不是數(shù)據(jù)類(lèi)型。如果兩個(gè)值相同,則返回true值;如果兩個(gè)值不相同,則返回false值。

在Java中,“%”是取余的意思,是一個(gè)二元算術(shù)運(yùn)算符,可進(jìn)行除法運(yùn)算并獲取余數(shù),語(yǔ)法“操作數(shù)1 % 操作數(shù)2”。取余運(yùn)算符“%”的操作數(shù)通常是正整數(shù)也可以是負(fù)數(shù)甚至是浮點(diǎn)數(shù),如果負(fù)數(shù)參與此運(yùn)算,則結(jié)果的正負(fù)取決于前面一個(gè)數(shù)是正數(shù)還是負(fù)數(shù)。

python憑借其簡(jiǎn)單易讀的語(yǔ)法,廣泛應(yīng)用于廣泛的領(lǐng)域中。掌握Python語(yǔ)法的基礎(chǔ)結(jié)構(gòu)至關(guān)重要,既可以提高編程效率,又能深入理解代碼的運(yùn)作方式。為此,本文提供了一個(gè)全面的思維導(dǎo)圖,詳細(xì)闡述了Python語(yǔ)法的各個(gè)方面。變量和數(shù)據(jù)類(lèi)型變量是Python中用于存儲(chǔ)數(shù)據(jù)的容器。思維導(dǎo)圖展示了常見(jiàn)的Python數(shù)據(jù)類(lèi)型,包括整數(shù)、浮點(diǎn)數(shù)、字符串、布爾值和列表。每個(gè)數(shù)據(jù)類(lèi)型都有其自身的特性和操作方法。運(yùn)算符運(yùn)算符用于對(duì)數(shù)據(jù)類(lèi)型執(zhí)行各種操作。思維導(dǎo)圖涵蓋了Python中的不同運(yùn)算符類(lèi)型,例如算術(shù)運(yùn)算符、比

+=運(yùn)算符用于將左操作數(shù)的值加上右操作數(shù)的值,并將結(jié)果賦值給左操作數(shù),適用于數(shù)字類(lèi)型且左操作數(shù)必須可寫(xiě)。

在php中,可以使用“%”和“==”運(yùn)算符來(lái)判斷兩個(gè)數(shù)能否整除;只需要使用“%”運(yùn)算符將兩個(gè)數(shù)相除獲取余數(shù),再使用“==”運(yùn)算符判斷獲取的余數(shù)是否為0即可,語(yǔ)法“數(shù)1 % 數(shù)2 == 0”,如果為0則能整除,如果不為0則不能整除。

在Go語(yǔ)言中,運(yùn)算符按照優(yōu)先級(jí)從高到低的順序進(jìn)行計(jì)算。常見(jiàn)的運(yùn)算符的優(yōu)先級(jí)順序:1、括號(hào):()(最高優(yōu)先級(jí),用于強(qiáng)制改變運(yùn)算順序);2、?單目運(yùn)算符;3、乘性運(yùn)算符;4、加性運(yùn)算符;5、移位運(yùn)算符;6、按位運(yùn)算符;7、比較運(yùn)算符;8、邏輯運(yùn)算符;9、條件運(yùn)算符(三元運(yùn)算符);10、賦值運(yùn)算符等等。
