JavaScript check if variable exists (is defined/initialized)

Which method of checking if a variable has been initialized is better/correct? (Assuming the variable could hold anything (string, int, object, function, etc.)) if (elem) { // or !elem or if (typeof(elem) !== 'undefined') { or if (elem != null) { You want the typeof operator. Specifically: if (typeof variable !== 'undefined') { // the variable is defined } The typeof operator will c

JavaScript检查变量是否存在(已定义/初始化)

检查变量是否已被初始化的哪种方法更好/更正? (假设变量可以容纳任何东西(string,int,object,function等)) if (elem) { // or !elem 要么 if (typeof(elem) !== 'undefined') { 要么 if (elem != null) { 你想要typeof运算符。 特别: if (typeof variable !== 'undefined') { // the variable is defined } typeof运算符将检查变量是否真的未定义。 if (typeof variable === 'undefined') { // varia

How do I check for the existence of a variable

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers Just use typeof : console.log(typeof FooClass) // undefined console.log(typeof FooClass === 'undefined') // true I doubt there's any need to use language features specific to TypeScript.

我如何检查变量的存在

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 只需使用typeof : console.log(typeof FooClass) // undefined console.log(typeof FooClass === 'undefined') // true 我怀疑有没有必要使用特定于TypeScript的语言功能。

Check if variable is defined javascript

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers 使用typeof if(typeof variable === "undefined"){ /* It is undefined */ }

检查变量是否定义了javascript

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 使用typeof if(typeof variable === "undefined"){ /* It is undefined */ }

What is the best way to check if a variable is set?

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers check it using typeof if(typeof(a) != "undefined") { //code goes here } Here are some related questions. How can I check whether a variable is defined in JavaScript? Test if a variable is defined in javascript? if (typeof a !="undefined") { //write your code here }

检查变量是否设置的最佳方法是什么?

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 使用typeof检查它 if(typeof(a) != "undefined") { //code goes here } 这里有一些相关的问题。 我怎样才能检查一个变量是否在JavaScript中定义? 测试一个变量是否在JavaScript中定义? if (typeof a !="undefined") { //write your code here }

Uncaught ReferenceError: Notification is not defined

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers You can use typeof like so: if (typeof Notification !== 'undefined') { } If you use typeof , it does not try to actually use the variable (which breaks and throws an error)

未捕获的ReferenceError:通知未定义

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 你可以像这样使用typeof : if (typeof Notification !== 'undefined') { } 如果使用typeof ,它不会尝试实际使用该变量(这会中断并引发错误)

Check if variable is undefined not working

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers 使用这样的东西来验证变量是否未定义if (typeof something === "undefined") { alert("something is undefined"); }

检查变量是否未定义不起作用

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 使用这样的东西来验证变量是否未定义if (typeof something === "undefined") { alert("something is undefined"); }

Javascript array value is undefined ... how do I test for that

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers array[index] == 'undefined' compares the value of the array index to the string "undefined". You're probably looking for typeof array[index] == 'undefined' , which compares the type. You are checking it the array index contains a string "unde

Javascript数组的值是未定义的...我该如何测试

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 array[index] == 'undefined'将数组索引的值与字符串“undefined”进行比较。 你可能正在寻找typeof array[index] == 'undefined' ,它比较了类型。 您正在检查数组索引是否包含字符串"undefined" ,您应该使用typeof运算符: typeof predQuery[preId] == 'undefined' 或者使用undefined全局属性: predQu

JS: How to check if a variable is NOT undefined

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers var lastname = "Hi"; if(typeof lastname !== "undefined") { alert("Hi. Variable is defined."); }

JS:如何检查一个变量是不是未定义的

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 var lastname = "Hi"; if(typeof lastname !== "undefined") { alert("Hi. Variable is defined."); }

How can I determine if a JavaScript variable is defined in a page?

This question already has an answer here: JavaScript check if variable exists (is defined/initialized) 28 answers 我得到它使用if (typeof(x) != "undefined") 为了避免意外的分配,我习惯颠倒条件表达式的顺序: if ('undefined' !== typeof x) { 与其他运算符不同,typeof运算符在与未声明的符号一起使用时不会抛出ReferenceError异常,因此它可以安全地使用... if (typeof a != "undefined") { a(); }

如何确定JavaScript变量是否在页面中定义?

这个问题在这里已经有了答案: JavaScript检查变量是否存在(已定义/初始化)28个答案 我得到它使用if (typeof(x) != "undefined") 为了避免意外的分配,我习惯颠倒条件表达式的顺序: if ('undefined' !== typeof x) { 与其他运算符不同,typeof运算符在与未声明的符号一起使用时不会抛出ReferenceError异常,因此它可以安全地使用... if (typeof a != "undefined") { a(); }

Compare JavaScript values of different type (not strings)?

How can I find all items in an array by matching a property and dealing with case insensitivity ONLY IF the values are strings? I have no way to know what data type the property on the object will be. Both the target value and property may be a date, string, number, etc. I'm basically trying to protect the next developer from shooting himself in the foot: function getItemsByKey(key, valu

比较不同类型的JavaScript值(不是字符串)?

如何通过匹配属性和处理大小写不敏感来查找数组中的所有项目?如果值是字符串? 我无法知道对象上的属性是什么数据类型。 目标值和属性都可以是日期,字符串,数字等。 我基本上试图保护下一个开发者不让自己在脚下开枪: function getItemsByKey(key, value, isCaseSensitive) { var result = []; (getAll() || []).forEach(function(item){ if (!(!!isCaseSensitive)) { if (item[key] &&