Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nullish coalesing

Expression:
	Left ?? Right
if left is null or undefined , then Right will be the value 

const a = '' ;
const b = undefined;

const c = a ?? 'default' ; // will give c =''
const d = b ?? 'default' ; // wil give d = 'default'
Comment

Nullish Coalescing Vs Logical OR opreators

console.log(<left-operand> <operator> <right-operand>);

// same behaviour
console.log(undefined || "John");	// "John"
console.log(undefined ?? "John");	// "John"
            
console.log(null || "John");	// "John"
console.log(null ?? "John");	// "John"

// different behaviour
console.log(0 || "John");	// "John"
console.log(0 ?? "John");	// 0
            
console.log("" || "John");	// "John"
console.log("" ?? "John");	// ""

console.log(false || "John");	// "John"
console.log(false ?? "John");	// false
Comment

nullish coalescing operator

//nullish coalescing operator in js
Expression:
	Left ?? Right
if left is null or undefined , then Right will be the value

let value = null ?? "Oops.. null or undefined";
console.log(value) //Oops.. null or undefined

value = undefined ?? "Oops.. null or undefined";
console.log(value) //Oops.. null or undefined

value = 25 ?? "Oops.. null or undefined";
console.log(value) // 25

value = "" ?? "Oops.. null or undefined";
console.log(value) // ""
Comment

nullish coalescing operator

(null || undefined) ?? "toto"; // Renvoie "toto"
Comment

nullish-coalescing-operator

null || undefined ?? "foo"; // raises a SyntaxError
true || undefined ?? "foo"; // raises a SyntaxError
Comment

Nullish Coalescing

let x = foo ?? bar(); // return foo if it's not null or undefined otherwise calculate bar
Comment

Nullish coalescing operator

alert(username ?? "Guest");
Comment

nullish-coalescing-operator

let foo = { someFooProp: "hi" };

console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI"
console.log(foo.someBarProp?.toUpperCase() ?? "not available"); // "not available"
Comment

PREVIOUS NEXT
Code Example
Javascript :: js jwt decode 
Javascript :: is js object oriented 
Javascript :: express send pdf to view 
Javascript :: how to install moralis and react-moralis 
Javascript :: difference between normal function and arrow function 
Javascript :: how to add id in jquery 
Javascript :: js commenst 
Javascript :: javascript Arrow Function with One Argumen 
Javascript :: run for loop every second js 
Javascript :: mdn includes 
Javascript :: javascript read word document 
Javascript :: array length in javascript 
Javascript :: rich text react renderer 
Javascript :: mean stack 
Javascript :: redux saga use navigation 
Javascript :: node fs existssync 
Javascript :: date in javascript 
Javascript :: grid in chart.js 
Javascript :: javascript loops 
Javascript :: prepen an element js 
Javascript :: sequelize contains 
Javascript :: javascript update value when slider moves javascript 
Javascript :: how to create an array in javascript 
Javascript :: jquery copy to clipboard 
Javascript :: web3 connect to smart contract 
Javascript :: sequelize transaction util 
Javascript :: javascript navigator.mediaDevices.getUserMedia 
Javascript :: experss cookie session 
Javascript :: find object in array 
Javascript :: jquery effect methods 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =