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/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390959.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390959.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390959.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

asp.net讀取用戶控件,自定義加載用戶控件

1、自定義加載用戶控件 ceshi.aspx頁面 <html><body> <div id"divControls" runat"server"></div> </body></html> ceshi.aspx.cs頁面 System.Web.UI.UserControl newUC (System.Web.UI.UserControl)Page.LoadContro…

配置Java_Home,臨時環境變量信息

一、內容回顧 上一篇博客《Java運行環境的搭建---Windows系統》 我們說到了配置path環境變量的目的在于控制臺可以在任意路徑下都可以找到java的開發工具。 二、配置其他環境變量 1. 原因 為了獲取更大的用戶群體&#xff0c;所以使用java語言開發系統需要兼容不同版本的jdk&a…

網頁縮放與窗口縮放_功能縮放—不同的Scikit-Learn縮放器的效果:深入研究

網頁縮放與窗口縮放內部AI (Inside AI) In supervised machine learning, we calculate the value of the output variable by supplying input variable values to an algorithm. Machine learning algorithm relates the input and output variable with a mathematical func…

在構造器里調用可重寫的方法有什么問題?

問題&#xff1a;在構造器里調用可重寫的方法有什么問題&#xff1f; 我有一個檢票頁面的類通過抽象方法的結果去去設置頁的標題 public abstract class BasicPage extends WebPage {public BasicPage() {add(new Label("title", getTitle()));}protected abstract…

創建hugo博客_如何創建您的第一個Hugo博客:實用指南

創建hugo博客Hugo is a great tool to use if you want to start a blog.如果您想創建博客&#xff0c;Hugo是一個很好的工具。 I use Hugo myself for my blog, flaviocopes.com, and Ive been using it for more than two years. I have a few reasons for loving Hugo.我本…

Python自動化開發01

一、 變量變量命名規則變量名只能是字母、數字或下劃線的任意組合變量名的第一個字符不能是數字以下關鍵字不能聲明為變量名 [and, as, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not,…

記錄關于vs2008 和vs2015 的報錯問題

出現了 VS2008無法創建項目&#xff0c;無法打開項目的情況&#xff0c;提示這個注冊表鍵值有問題 HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ MSBuild \ ToolsVersions \ 14.0&#xff0c; 但是查看了注冊表沒有這個鍵。費盡辛萬苦&#xff0c;中午在思密達的一個網站上看到…

未越獄設備提取數據_從三星設備中提取健康數據

未越獄設備提取數據Health data is collected every time you have your phone in your pocket. Apple or Android, the phones are equipped with a pedometer that counts your steps. Hence, health data is recorded. This data could be your one free data mart for a si…

怎么樣用System.out.println在控制臺打印出顏色

問題&#xff1a;怎么樣用System.out.println在控制臺打印出顏色 怎么樣才能在控制臺里打印顏色啊&#xff1f;我想要展示一些有顏色的字體&#xff0c;當處理器發送數據和接收數據的時候&#xff0c;也使用不同顏色的字體。 回答一 在這個Java類里面帶有public static 的數…

sql注入語句示例大全_SQL Order By語句:示例語法

sql注入語句示例大全Order By is a SQL command that lets you sort the resulting output from a SQL query.Order By是一個SQL命令&#xff0c;可讓您對SQL查詢的結果輸出進行排序。 訂購依據(ASC&#xff0c;DESC) (Order By (ASC, DESC)) ORDER BY gives us a way to SORT…

[BZOJ2599][IOI2011]Race 點分治

2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 3934 Solved: 1163[Submit][Status][Discuss]Description 給一棵樹,每條邊有權.求一條簡單路徑,權值和等于K,且邊的數量最小.N < 200000, K < 1000000 Input 第一行 兩個整數 n, k第二..n行 每行三…

分詞消除歧義_角色標題消除歧義

分詞消除歧義折磨數據&#xff0c;它將承認任何事情 (Torture the data, and it will confess to anything) Disambiguation as defined in the vocabulary.com dictionary refers to the removal of ambiguity by making something clear and narrowing down its meaning. Whi…

北航教授李波:說AI會有低潮就是胡扯,這是人類長期的追求

這一輪所謂人工智能的高潮&#xff0c;和以往的幾次都有所不同&#xff0c;那是因為其受到了產業界的極大關注和參與。而以前并不是這樣。 當今世界是一個高度信息化的世界&#xff0c;甚至我們有一只腳已經踏入了智能化時代。而在我們日常交流和信息互動中&#xff0c;迅速發…

創建字符串枚舉的最好方法

問題&#xff1a;創建字符串枚舉的最好方法 用一個枚舉類型去表示一組字符串的最好方法是什么 我嘗試這樣&#xff1a; enum Strings{STRING_ONE("ONE"), STRING_TWO("TWO") }我怎么樣才可以像使用字符串那樣使用它們&#xff1f; 回答一 我不知道你想…

網絡安全習慣_健康習慣,確保良好的網絡安全

網絡安全習慣In a similar fashion to everyone getting the flu now and again, the risk of catching a cyberattack is a common one. Both a sophisticated social engineering attack or grammatically-lacking email phishing scam can cause real damage. No one who c…

attr和prop的區別

由于prop(property的縮寫)和attr(attribute的縮寫)翻譯成漢語&#xff0c;均有“特性、屬性”等意思的原因&#xff0c;導致大家容易混淆分不清。 (1)在處理自定義時屬性時&#xff0c;用attr()&#xff0c;若用prop(),則結果為undefined&#xff1b; (2)DOM固有屬性&#xff0…

15行Python代碼,幫你理解令牌桶算法

在網絡中傳輸數據時&#xff0c;為了防止網絡擁塞&#xff0c;需限制流出網絡的流量&#xff0c;使流量以比較均勻的速度向外發送&#xff0c;令牌桶算法就實現了這個功能&#xff0c;可控制發送到網絡上數據的數目&#xff0c;并允許突發數據的發送。 什么是令牌 從名字上看令…

在Java中,如何使一個字符串的首字母變為大寫

問題&#xff1a;在Java中&#xff0c;如何使一個字符串的首字母變為大寫 我使用Java去獲取用戶的字符串輸入。我嘗試使他們輸入的第一個字符大寫 我嘗試這樣: String name;BufferedReader br new InputStreamReader(System.in);String s1 name.charAt(0).toUppercase());…

在加利福尼亞州投資于新餐館:一種數據驅動的方法

“It is difficult to make predictions, especially about the future.”“很難做出預測&#xff0c;尤其是對未來的預測。” ~Niels Bohr?尼爾斯波爾 Everything is better interpreted through data. And data-driven decision making is crucial for success in any ind…

javascript腳本_使用腳本src屬性將JavaScript鏈接到HTML

javascript腳本The ‘src’ attribute in a tag is the path to an external file or resource that you want to link to your HTML document.標記中的src屬性是您要鏈接到HTML文檔的外部文件或資源的路徑。 For example, if you had your own custom JavaScript file named …