日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JavaScript数据类型:Typeof解释

發布時間:2023/11/29 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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是一個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/

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的JavaScript数据类型:Typeof解释的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。