null-check

JavaScript null check

蹲街弑〆低调 提交于 2019-11-27 16:53:14
I've come across the following code: function test(data) { if (data != null && data !== undefined) { // some code here } } I'm somewhat new to JavaScript, but, from other questions I've been reading here, I'm under the impression that this code does not make much sense. In particular, this answer states that You'll get an error if you access an undefined variable in any context other than typeof . Update: The (quote of the) answer above may be misleading. It should say «an undeclared variable» , instead of «an undefined variable» . As I found out, in the answers by Ryan ♦ , maerics , and

Comparing a generic against null that could be a value or reference type?

江枫思渺然 提交于 2019-11-27 10:59:44
问题 public void DoFoo<T>(T foo) where T : ISomeInterface<T> { //possible compare of value type with 'null'. if (foo == null) throw new ArgumentNullException("foo"); } I'm purposely only checking against null because I don't want to restrict a ValueType from being equal to its default(T) . My code compiles and works just fine this way (ReSharper complains, but not CodeAnalysis). Though I do wonder: Is there a more standard way to handle this situation? Is there any chance of an issue arrising from

How to check if a variable is not null?

随声附和 提交于 2019-11-27 05:50:11
I know that below are the two ways in JavaScript to check whether a variable is not null , but I’m confused which is the best practice to use. Should I do: if (myVar) {...} or if (myVar !== null) {...} They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null . The only values that are not truthy in JavaScript are the following (a.k.a. falsy values): null undefined 0 "" (the empty string) false NaN Tolgahan Albayrak Here is how

JavaScript null check

*爱你&永不变心* 提交于 2019-11-26 18:47:32
问题 I've come across the following code: function test(data) { if (data != null && data !== undefined) { // some code here } } I'm somewhat new to JavaScript, but, from other questions I've been reading here, I'm under the impression that this code does not make much sense. In particular, this answer states that You'll get an error if you access an undefined variable in any context other than typeof . Update: The (quote of the) answer above may be misleading. It should say «an undeclared variable

How to check if a variable is not null?

空扰寡人 提交于 2019-11-26 11:45:34
问题 I know that below are the two ways in JavaScript to check whether a variable is not null , but I’m confused which is the best practice to use. Should I do: if (myVar) {...} or if (myVar !== null) {...} 回答1: They are not equivalent. The first will execute the block following the if statement if myVar is truthy (i.e. evaluates to true in a conditional), while the second will execute the block if myVar is any value other than null . The only values that are not truthy in JavaScript are the