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
是一個JavaScript關鍵字,當您調用它時將返回變量的類型。 您可以使用它來驗證函數參數或檢查是否定義了變量。 還有其他用途。
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
運算符很有用,因為它是檢查代碼中變量類型的簡便方法。 這很重要,因為JavaScript是一種動態類型的語言 。 這意味著在創建變量時不需要為變量分配類型。 因為不以此方式限制變量,所以其類型可以在程序運行時更改。
For example:
例如:
var x = 12345; // number
x = 'string'; // string
x = { key: 'value' }; // object
As 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中的變量可以在程序執行期間更改類型。 作為程序員可能很難跟蹤,這就是typeof
運算符有用的地方。
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
運算符返回一個表示變量當前類型的字符串。 您可以通過鍵入typeof(variable)
或typeof variable
來使用它。 回到上一個示例,您可以在每個階段使用它來檢查變量x
的類型:
var x = 12345;
console.log(typeof x) // number
x = 'string';
console.log(typeof x) // string
x = { key: 'value' };
console.log(typeof x) // object
This can be useful for checking the type of a variable in a function and continuing as appropriate.
這對于檢查函數中變量的類型并酌情繼續操作很有用。
Here’s an example of a function that can take a variable that is a string or a number:
這是一個函數示例,該函數可以采用字符串或數字作為變量:
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
運算符有用的另一種方式是,在嘗試在代碼中訪問變量之前,確保已定義了變量。 如果您嘗試訪問未定義的變量,這可以幫助防止程序中可能發生的錯誤。
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.
當檢查數字時, typeof
運算符的輸出可能并不總是您期望的。 數字可能由于多種原因而變成值NaN(非數字) 。
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.
也許您試圖將一個對象與一個數字相乘,因為您忘記了訪問該對象內部的數字。
var x = 1;
var y = { number: 2 };
console.log(x * y); // NaN
console.log(typeof (x * y)); // number
When 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.
當一個號碼的檢查,這是不夠的,檢查的輸出typeof
的數量,因為NaN
alsopasses的人數這個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.
即使認為這是一種有用的驗證方法,我們也要小心,因為javascript有一些奇怪的部分,其中之一是typeof
over特定指令的結果。 例如,在javascript中,很多東西都只是objects
所以您會發現。
var x = [1,2,3,4];
console.log(typeof x) // objectconsole.log(typeof null) // object
更多信息: (More Information:)
MDN Documentation for typeof
有關typeof的MDN文檔
翻譯自: https://www.freecodecamp.org/news/javascript-data-types-typeof-explained/