Quantcast
Channel: Is there a "null coalescing" operator in JavaScript? - Stack Overflow
Browsing latest articles
Browse All 20 View Live

Answer by EliteRaceElephant for Is there a "null coalescing" operator in...

Chain multiple values / several values"short circuit" is enabled: do not evaluate any further if one of the first values is validthat means order matters, the most left values are prioritizedconst...

View Article



Answer by Basil Abbas for Is there a "null coalescing" operator in JavaScript?

I was trying to check if an input is null and then use the value accordingly. This is my code.let valueToBeConsidered = !inputValue ? "trueCondition" : "falseCondition",So if inputValue is null then...

View Article

Answer by Inigo for Is there a "null coalescing" operator in JavaScript?

?? vs || vs &&None of the other answers compares all three of these. Since Justin Johnson's comment has so many votes, and since double question mark vs && in javascript was marked a...

View Article

Answer by Ran Turner for Is there a "null coalescing" operator in JavaScript?

ECMAScript 2021 enabled two new features:Nullish coalescing operator (??) which is a logical operator that returns its right-hand side operand when its left-hand side operand is either null or...

View Article

Answer by Michael Freidgeim for Is there a "null coalescing" operator in...

Those who are using Babel, need to upgrade to the latest version to use nullish coalescing (??):Babel 7.8.0 supports the new ECMAScript 2020 features by default: youdon't need to enable individual...

View Article


Answer by Amit for Is there a "null coalescing" operator in JavaScript?

There are two items here:Logical ORconst foo = '' || 'default string';console.log(foo); // output is 'default string'Nullish coalescing operatorconst foo = '' ?? 'default string';console.log(foo); //...

View Article

Answer by Rm558 for Is there a "null coalescing" operator in JavaScript?

Need to support old browser and have a object hierarchybody.head.eyes[0] //body, head, eyes may be null may use this,(((body||{}) .head||{}) .eyes||[])[0] ||'left eye'

View Article

Answer by Gibolt for Is there a "null coalescing" operator in JavaScript?

Logical nullish assignment, 2020+ solutionA new operator is currently being added to the browsers, ??=. This combines the null coalescing operator ?? with the assignment operator =.NOTE: This is not...

View Article


Answer by rajesh kumar for Is there a "null coalescing" operator in JavaScript?

Now it has full support in latest version of major browsers like Chrome, Edge, Firefox , Safari etc. Here's the comparison between the null operator and Nullish Coalescing Operatorconst response = {...

View Article


Answer by faithfull for Is there a "null coalescing" operator in JavaScript?

Yes, and its proposal is Stage 4 now. This means that the proposal is ready for inclusion in the formal ECMAScript standard. You can already use it in recent desktop versions of Chrome, Edge and...

View Article

Answer by Vandesh for Is there a "null coalescing" operator in JavaScript?

It will hopefully be available soon in Javascript, as it is in proposal phase as of Apr, 2020. You can monitor the status here for compatibility and support -...

View Article

Answer by Lars Blumberg for Is there a "null coalescing" operator in JavaScript?

Note that React's create-react-app tool-chain supports the null-coalescing since version 3.3.0 (released 5.12.2019). From the release notes:Optional Chaining and Nullish Coalescing OperatorsWe now...

View Article

Answer by vaughan for Is there a "null coalescing" operator in JavaScript?

Yes, it is coming soon. See proposal here and implementation status here.It looks like this:x ?? yExampleconst response = { settings: { nullValue: null, height: 400, animationDuration: 0, headerText:...

View Article


Answer by Kevin Nelson for Is there a "null coalescing" operator in JavaScript?

Nobody has mentioned in here the potential for NaN, which--to me--is also a null-ish value. So, I thought I'd add my two-cents.For the given code:var a, b = null, c = parseInt('Not a number'), d = 0, e...

View Article

Answer by Brent Larsen for Is there a "null coalescing" operator in JavaScript?

function coalesce() { var len = arguments.length; for (var i=0; i<len; i++) { if (arguments[i] !== null && arguments[i] !== undefined) { return arguments[i]; } } return null;}var xyz =...

View Article


Answer by sth for Is there a "null coalescing" operator in JavaScript?

If || as a replacement of C#'s ?? isn't good enough in your case, because it swallows empty strings and zeros, you can always write your own function: function $N(value, ifnull) { if (value === null ||...

View Article

Answer by farzad for Is there a "null coalescing" operator in JavaScript?

beware of the JavaScript specific definition of null. there are two definitions for "no value" in javascript.1. Null: when a variable is null, it means it contains no data in it, but the variable is...

View Article


Answer by Ates Goral for Is there a "null coalescing" operator in JavaScript?

UpdateJavaScript now supports the nullish coalescing operator (??). It returns its right-hand-side operand when its left-hand-side operand is null or undefined, and otherwise returns its left-hand-side...

View Article

Answer by Tom for Is there a "null coalescing" operator in JavaScript?

After reading your clarification, @Ates Goral's answer provides how to perform the same operation you're doing in C# in JavaScript.@Gumbo's answer provides the best way to check for null; however, it's...

View Article

Is there a "null coalescing" operator in JavaScript?

Is there a null coalescing operator in Javascript?For example, in C#, I can do this:String someString = null;var whatIWant = someString ?? "Cookies!";The best approximation I can figure out for...

View Article
Browsing latest articles
Browse All 20 View Live




Latest Images