Why is there a `null` value in JavaScript?

In JavaScript, there are two values which basically say 'I don't exist' - undefined and null . A property to which a programmer has not assigned anything will be undefined , but in order for a property to become null , null must be explicitly assigned to it. I once thought that there was a need for null because undefined is a primitive value and null an object. It's not, even

为什么JavaScript中有一个`null`值?

在JavaScript中,有两个基本上说'我不存在'的值 - undefined和null 。 程序员没有分配任何东西的属性将是undefined ,但为了使属性变为null ,必须明确地将null分配给它。 我曾经认为需要null因为undefined是一个原始值, null是一个对象。 它不是,即使typeof null会产生'object' :实际上,它们都是原始值 - 这意味着从构造函数函数中既不会返回undefined也不会返回null ,因为两者都会被转换为空对象(

Center Javascript Slideshow?

This question already has an answer here: How to horizontally center a <div> in another <div>? 82 answers You just need to put <div> around your slideshow and apply text-align:center to the div. <div style="text-align:center;"> <img src="congo.jpg" name="slide" width="400" height="300"> </div> Quick and dirty example: https://jsfiddle.net/pbsz9spu/

中心JavaScript幻灯片?

这个问题在这里已经有了答案: 如何在另一个<div>中水平居中<div>? 82个答案 您只需将<div>放在幻灯片上,然后将text-align:center应用于div。 <div style="text-align:center;"> <img src="congo.jpg" name="slide" width="400" height="300"> </div> 快速和肮脏的例子:https://jsfiddle.net/pbsz9spu/ /*css*/ .slideshowContainer { position:relative; width:400px; hei

Input names that should not be used?

Earlier, I was creating a form and mindlessly named one of my inputs "name", similar to the following pseudo code: <input id="name" type="text" name="name" /> I then remembered that in the past, I've had issues with inputs with the name "action." If I remember correctly, the issues arose during enhancement with JavaScript (there was confusion between the form'

输入不应该使用的名称?

之前,我正在创建一个表单,并且无意识地命名了我的一个输入“名称”,类似于以下伪代码: <input id="name" type="text" name="name" /> 然后我想起了过去,我有一些问题与“行动”这个名字有关。 如果我没有记错,那么在使用JavaScript进行增强时就会出现这些问题(表单操作与名称为“action”的输入之间存在混淆)。 所以我的问题是: 使用类似于上面代码段的内容安全吗? 是否有一些通常不应该用作输入名称的规范的

JavaScript case insensitive string comparison

如何在JavaScript中执行不区分大小写的字符串比较? 最简单的方法(如果你不担心特殊的Unicode字符)就是调用toUpperCase : var areEqual = string1.toUpperCase() === string2.toUpperCase(); The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the 'i' flag. JavaScript: case-insensitive search When both strings being compared are variables (not cons

不区分大小写的JavaScript字符串比较

如何在JavaScript中执行不区分大小写的字符串比较? 最简单的方法(如果你不担心特殊的Unicode字符)就是调用toUpperCase : var areEqual = string1.toUpperCase() === string2.toUpperCase(); 在JavaScript中进行不区分大小写的比较的最佳方式是使用带有'i'标志的RegExp match()方法。 JavaScript:不区分大小写的搜索 当两个被比较的字符串都是变量(而不是常量)时,它会更复杂一些,因为您需要从字符串中生成

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value.length == 0 inside of an if statement. Is there a performance benefit to replacing == with === ? Any performance improvement would be welcomed as many comparison operators exist. If no ty

应该在JavaScript比较中使用哪个等于运算符(== vs ===)?

我使用JSLint来浏览JavaScript,并且它返回了很多建议,以便在比较idSele_UNVEHtype.value.length == 0时使用=== (三个等号)替换== (两个等号) if声明。 用===替换==是否有性能优势? 许多比较运算符存在,任何性能改进都会受到欢迎。 如果没有类型转换发生,会有比==更好的性能吗? 身份( === )运算符的行为与相等( == )运算符的行为相同,除非没有进行类型转换,并且类型必须相同才能被视为相等。 参考:Jav

how can I compare a string with several values?

Possible Duplicate: array.contains(obj) in JavaScript Something like: if (mystring == "a" || mystring == "b" || mystring =="c") I was hopping to do: if (mystring in ("a", "b", "c")) is it possible? You could use indexOf() like this if ( [ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... } EDIT With ES7 comes a little simplification. As of today just Chrome 47+ and FF 43+ seem to s

我怎样才能比较一个字符串与几个值?

可能重复: array.contains(obj)在JavaScript中 就像是: if (mystring == "a" || mystring == "b" || mystring =="c") 我正在跳跃做: if (mystring in ("a", "b", "c")) 可能吗? 你可以像这样使用indexOf() if ( [ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... } 编辑与ES7来了一点简化。 截至今天,Chrome 47+和FF 43+似乎都支持它: if ( [ 'a', 'b', 'c' ].includes( mystring ) ) { ... } Arra

Find out if a variable is in an array?

This question already has an answer here: How do I check if an array includes an object in JavaScript? 40 answers jQuery has a utility function to find whether an element exist in array or not $.inArray(value, array) It returns index of the value in array and -1 if value is not present in array. so your code can be like this if( $.inArray(code, countryList) != -1){ alert('value is A

找出一个变量是否在数组中?

这个问题在这里已经有了答案: 如何检查数组是否包含JavaScript中的对象? 40个答案 jQuery有一个实用功能来查找数组中是否存在元素 $.inArray(value, array) 它返回array中的值的索引,如果array中不存在值,则返回-1 。 所以你的代码可以是这样的 if( $.inArray(code, countryList) != -1){ alert('value is Array!'); } else { alert('Not an array'); } 你需要使用Array.indexOf : if (countryList.inde

Javascript if in x

Possible Duplicates: Test for value in Javascript Array Best way to find an item in a JavaScript Array ? Javascript - array.contains(obj) I usually program in python but have recently started to learn JavaScript. In python this is a perfectly valid if statement: list = [1,2,3,4] x = 3 if x in list: print "It's in the list!" else: print "It's not in the list!" but I have had po

Javascript如果在x中

可能重复: 测试Javascript数组中的值 在JavaScript数组中找到项目的最佳方法? Javascript - array.contains(obj) 我通常用python编程,但最近开始学习JavaScript。 在Python中,这是一个非常有效的if语句: list = [1,2,3,4] x = 3 if x in list: print "It's in the list!" else: print "It's not in the list!" 但我曾在Javascript中做过同样的事情。 你如何检查x是否在JavaScript中的列表y?

check if value exists in array

This question already has an answer here: How do I check if an array includes an object in JavaScript? 40 answers var codes = ["US-AL", "US-AZ", ... ]; if($.inArray(code, codes) > -1){ //do something } 更新:结合@ Deestan的建议,这可以重写为.. function isValidCode(code){ return ($.inArray(code, codes) > -1); } 是的 array.indexOf(searchElement[, fromIndex])

检查数组中是否存在值

这个问题在这里已经有了答案: 如何检查数组是否包含JavaScript中的对象? 40个答案 var codes = ["US-AL", "US-AZ", ... ]; if($.inArray(code, codes) > -1){ //do something } 更新:结合@ Deestan的建议,这可以重写为.. function isValidCode(code){ return ($.inArray(code, codes) > -1); } 是的 array.indexOf(searchElement[, fromIndex])

check array for value

This question already has an answer here: How do I check if an array includes an object in JavaScript? 40 answers If you don't care about legacy browsers: if ( bank_holidays.indexOf( '06/04/2012' ) > -1 ) if you do care about legacy browsers, there is a shim available on MDN. Otherwise, jQuery provides an equivalent function: if ( $.inArray( '06/04/2012', bank_holidays ) > -1 )

检查数组的值

这个问题在这里已经有了答案: 如何检查数组是否包含JavaScript中的对象? 40个答案 如果你不关心传统浏览器: if ( bank_holidays.indexOf( '06/04/2012' ) > -1 ) 如果您关心的是旧版浏览器,则MDN上有一个填充程序。 否则,jQuery提供了一个等价的函数: if ( $.inArray( '06/04/2012', bank_holidays ) > -1 ) 尝试这个: // this will fix old browsers if (!Array.prototype.indexOf) { Array.prototype.i