How to set a variable passed on to a function as null in JavaScript

Suppose I have a function in JavaScript: function something (variable) { console.log(variable); } How do I set that if there is no variable passed, then it is null by default? JavaScript isn't very picky when it comes to the number of required function arguments when you call a function; any arguments that are mentioned in the declaration but not passed will be set to the type undefi

如何在JavaScript中将传递给函数的变量设置为null

假设我在JavaScript中有一个函数: function something (variable) { console.log(variable); } 我如何设置,如果没有变量通过,那么它默认为空? 当你调用一个函数时,对于所需函数参数的数量,JavaScript并不是很挑剔; 声明中提到但未传递的任何参数都将设置为undefined类型。 例如: function test(foo) { console.log(foo === undefined); // true } 要设置默认值,至少有三个选项: function test(foo) {

How to check empty object in angular 2 template using *ngIf

I want to check if my object is empty dont render my element, and this is my code: <div class="comeBack_up" *ngIf="previous_info != {}"> <a [routerLink]="['Inside_group_page',{'name':previous_info.path | dotTodash }]" > {{previous_info.title}} </a> </div> but my code is wrong, what is the best way to do this? This should do what you want: &l

如何使用* ngIf检查角2模板中的空对象

我想检查我的对象是否为空,不要渲染我的元素,这是我的代码: <div class="comeBack_up" *ngIf="previous_info != {}"> <a [routerLink]="['Inside_group_page',{'name':previous_info.path | dotTodash }]" > {{previous_info.title}} </a> </div> 但我的代码是错误的,最好的办法是什么? 这应该做你想做的事情: <div class="comeBack_up" *ngIf="(previous_info

Checking something isEmpty in Javascript?

How can I check if a variable is empty in Javascript? Sorry for the stupid question, but I'm a newbie in Javascript! if(response.photo) is empty { do something else { do something else } response.photo was from JSON, and it could be empty sometimes, empty data cells! I want to check if it's empty. If you're testing for an empty string: if(myVar === ''){ // do stuff };

在Javascript中检查一些东西是否是空的?

我怎样才能检查一个变量是否在Javascript中为空? 对不起,这个愚蠢的问题,但我是一个JavaScript的新手! if(response.photo) is empty { do something else { do something else } response.photo来自JSON,有时候它可能是空的,空的数据单元! 我想检查它是否是空的。 如果你正在测试一个空字符串: if(myVar === ''){ // do stuff }; 如果您正在检查已声明但尚未定义的变量: if(myVar === null){ // do stu

Check if variable is undefined

This question already has an answer here: How to determine if variable is 'undefined' or 'null'? 24 answers 未定义是js中的虚假...它看起来像bar.baz可能是你的罪魁祸首。 You can use typeof to check if a variable is undefined . It always returns a string . if (typeof foo === 'undefined') { console.log('foo is undefined'); } var foo = ['one', 'two', 'three']; if (typeo

检查变量是否未定义

这个问题在这里已经有了答案: 如何确定变量是'undefined'还是'null'? 24个答案 未定义是js中的虚假...它看起来像bar.baz可能是你的罪魁祸首。 您可以使用typeof来检查变量是否undefined 。 它总是返回一个string 。 if (typeof foo === 'undefined') { console.log('foo is undefined'); } var foo = ['one', 'two', 'three']; if (typeof foo !== 'undefined') { // access elements c

!== vs !=

Ok, so I installed Linter on my Sublime editor while working on my node.js app. One of the things that it caught said that I should always use !== to compare an object to null (I usually use != ). So I changed it...but then I noticed that the !== wasn't working. I have this scenario: var x = null; if (x !== null) console.log('x is not equal to null'); When I use the !== the console

!== vs!=

好的,所以我在使用我的Node.js应用程序时在我的Sublime编辑器上安装了Linter。 它发现的其中一件事说我应该总是使用!==来比较一个对象为null(我通常使用!=)。 所以我改变了它......但后来我注意到!==不工作。 我有这种情况: var x = null; if (x !== null) console.log('x is not equal to null'); 当我使用!==控制台打印该行时,即使它显然不是真的。 当我将它切换回!=它表现正常。 所以我的问题是,为

JavaScript: What is the difference between `if (!x)` and `if (x == null)`?

What is the difference between if (!x) and if (x == null) ; that is, when can their results be different? !x will return true for every "falsy" value (empty string, 0 , null , false , undefined , NaN ) whereas x == null will only return true if x is null (edit: or apparently undefined (see below)). Try with x = 0 , there is a difference. You can say that the NOT operator ! conve

JavaScript:if(!x)`和if(x == null)`有什么区别?

if (!x)和if (x == null)之间的区别是什么? 也就是说,他们的结果何时会不同? !x将返回true为每一个“falsy”值(空字符串, 0 , null , false , undefined , NaN ),而x == null只会返回true ,如果x 为 null (编辑:或明显undefined (见下文))。 尝试x = 0 ,有一个区别。 你可以说那个NOT运算符! 将值转换为其相反的布尔等值。 这与实际比较两个值不同。 另外,如果你用==比较值,JavaScript会进行类型转

Every Object is a function and every function is Object

I was reading this link JavaScript_syntax This seems to be cyclic - that every function is an Object and every Object itself is a function. Which is the atomic one? Can someone explain in a better way? Anything that is not a primitive type (undefined, null, number, string, boolean) is an object (or an instance) in JavaScript. That means function inherits from object . Object instances ca

每个对象都是一个函数,每个函数都是对象

我正在阅读这个链接JavaScript_syntax 这似乎是循环的 - 每个函数都是一个对象,每个对象本身都是一个函数。 原子是哪一个? 有人能以更好的方式解释吗? 任何不是原始类型(undefined,null,number,string,boolean)都是JavaScript中的对象(或实例)。 这意味着function从object继承。 对象实例可以包含更多可以是函数的实例。 这就是我们所说的“方法”(因为它有一个自动的this变量)。 由于不能“调用”每个对象

Is it better to return `undefined` or `null` from a javascript function?

I have a function which I have written which basically looks like this: function getNextCard(searchTerms) { // Setup Some Variables // Do a bunch of logic to pick the next card based on termed passed through what I'll call here as 'searchTerms' all of this logic is omitted because it's not important for my question. // ... // If we find a next card to give, than give it if (nextCardF

从javascript函数返回undefined或null是否更好?

我有一个我写的函数,基本如下所示: function getNextCard(searchTerms) { // Setup Some Variables // Do a bunch of logic to pick the next card based on termed passed through what I'll call here as 'searchTerms' all of this logic is omitted because it's not important for my question. // ... // If we find a next card to give, than give it if (nextCardFound) return nextCardFound; //

Why does !{}[true] evaluate to true in JavaScript?

{}[true] is [true] and ![true] should be false . So why does !{}[true] evaluate to true ? I believe that's because plain {}[true] is parsed as an empty statement block (not an object literal) followed by an array containing true , which is true . On the other hand, applying the ! operator makes the parser interpret {} as an object literal, so the following {}[true] becomes a member acc

为什么在JavaScript中将{} [true]评估为true?

{}[true]是[true] , ![true]应该是false 。 那么为什么!{}[true]评估为true ? 我相信那是因为plain {}[true]被解析为一个空语句块(不是对象字面),后面跟着一个包含true的数组,这是true 。 另一方面,应用! 运算符使解析器将{}解释为对象字面值,因此以下{}[true]将成为返回undefined的成员访问,并且!{}[true]确实为true (如!undefined为true )。 由于{}[true]不返回true ,但undefined ,并且undefined被评估

JavaScript checking for null vs. undefined and difference between == and ===

I know, I know there must be some threads covering this topic. But I used the search and didn't get the answer which fits my needs. So here we go: How do I check a variable if it's null or undefined and what is the difference between the null and undefined ? What is the difference between "==" and "===" (it's hard to search Google for === )? How do I check

JavaScript检查null与undefined以及==和===之间的区别

我知道,我知道必须有一些涵盖这个主题的主题。 但我使用了搜索功能,没有得到符合我需求的答案。 所以我们走吧: 如何检查变量是否为null或undefined以及null和undefined之间的区别是什么? “==”和“===”有什么区别(很难搜索谷歌=== )? 如何检查变量是否为null或undefined ... 变量为null : if (a === null) // or if (a == null) // but see note below ...但是请注意,如果a undefined ,后者也将是真实的。