javascript
JavaScript数据类型:Typeof解释
typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well.
typeof是一個(gè)JavaScript關(guān)鍵字,當(dāng)您調(diào)用它時(shí)將返回變量的類型。 您可以使用它來驗(yàn)證函數(shù)參數(shù)或檢查是否定義了變量。 還有其他用途。
The typeof operator is useful because it is an easy way to check the type of a variable in your code. This is important because JavaScript is a is a dynamically typed language. This means that you aren’t required to assign types to variables when you create them. Because a variable is not restricted in this way, its type can change during the runtime of a program.
typeof運(yùn)算符很有用,因?yàn)樗菣z查代碼中變量類型的簡便方法。 這很重要,因?yàn)镴avaScript是一種動(dòng)態(tài)類型的語言 。 這意味著在創(chuàng)建變量時(shí)不需要為變量分配類型。 因?yàn)椴灰源朔绞较拗谱兞?#xff0c;所以其類型可以在程序運(yùn)行時(shí)更改。
For example:
例如:
var x = 12345; // number x = 'string'; // string x = { key: 'value' }; // objectAs you can see from the above example, a variable in JavaScript can change types throughout the execution of a program. This can be hard to keep track of as a programmer, and this is where the typeof operator is useful.
從上面的示例可以看到,JavaScript中的變量可以在程序執(zhí)行期間更改類型。 作為程序員可能很難跟蹤,這就是typeof運(yùn)算符有用的地方。
The typeof operator returns a string that represents the current type of a variable. You use it by typing typeof(variable) or typeof variable. Going back to the previous example, you can use it to check the type of the variable x at each stage:
typeof運(yùn)算符返回一個(gè)表示變量當(dāng)前類型的字符串。 您可以通過鍵入typeof(variable)或typeof variable來使用它。 回到上一個(gè)示例,您可以在每個(gè)階段使用它來檢查變量x的類型:
var x = 12345; console.log(typeof x) // number x = 'string'; console.log(typeof x) // string x = { key: 'value' }; console.log(typeof x) // objectThis can be useful for checking the type of a variable in a function and continuing as appropriate.
這對于檢查函數(shù)中變量的類型并酌情繼續(xù)操作很有用。
Here’s an example of a function that can take a variable that is a string or a number:
這是一個(gè)函數(shù)示例,該函數(shù)可以采用字符串或數(shù)字作為變量:
function doSomething(x) {if(typeof(x) === 'string') {alert('x is a string')} else if(typeof(x) === 'number') {alert('x is a number')} }Another way the typeof operator can be useful is by ensuring that a variable is defined before you try to access it in your code. This can help prevent errors in a program that may occur if you try to access a variable that is not defined.
typeof運(yùn)算符有用的另一種方式是,在嘗試在代碼中訪問變量之前,確保已定義了變量。 如果您嘗試訪問未定義的變量,這可以幫助防止程序中可能發(fā)生的錯(cuò)誤。
function(x){if (typeof(x) === 'undefined') {console.log('variable x is not defined');return;}// continue with function here... }The output of the typeof operator might not always be what you expect when you check for a number.Numbers can turn in to the value NaN (Not A Number) for multiple reasons.
當(dāng)檢查數(shù)字時(shí), typeof運(yùn)算符的輸出可能并不總是您期望的。 數(shù)字可能由于多種原因而變成值NaN(非數(shù)字) 。
console.log(typeof NaN); //"number"Maybe you tried to multiply a number with an object because you forgot to access the number inside the object.
也許您試圖將一個(gè)對象與一個(gè)數(shù)字相乘,因?yàn)槟浟嗽L問該對象內(nèi)部的數(shù)字。
var x = 1; var y = { number: 2 }; console.log(x * y); // NaN console.log(typeof (x * y)); // numberWhen checking for a number, it is not sufficient to check the output of typeof for a number, since NaN alsopasses this test.This function check for numbers, and also doesn’t allow the NaN value to pass.
當(dāng)一個(gè)號(hào)碼的檢查,這是不夠的,檢查的輸出typeof的數(shù)量,因?yàn)镹aN alsopasses的人數(shù)這個(gè)test.This功能檢查,也不允許NaN值傳遞。
function isNumber(data) {return (typeof data === 'number' && !isNan(data)); }Even thought this is a useful validation method, we have to be careful because javascript has some weird parts and one of them is the result of typeof over particular instructions. For example, in javascript many things are just objects so you’ll find.
即使認(rèn)為這是一種有用的驗(yàn)證方法,我們也要小心,因?yàn)閖avascript有一些奇怪的部分,其中之一是typeof over特定指令的結(jié)果。 例如,在javascript中,很多東西都只是objects所以您會(huì)發(fā)現(xiàn)。
var x = [1,2,3,4]; console.log(typeof x) // objectconsole.log(typeof null) // object更多信息: (More Information:)
MDN Documentation for typeof
有關(guān)typeof的MDN文檔
翻譯自: https://www.freecodecamp.org/news/javascript-data-types-typeof-explained/
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的JavaScript数据类型:Typeof解释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到锅碗瓢盆什么意思
- 下一篇: javascript脚本_使用脚本src