Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

alert(username ?? "Guest");
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

alert(username ?? "Guest");
Comment

PREVIOUS NEXT
Code Example
Javascript :: regex repeat n times 
Javascript :: jquery check if exist 
Javascript :: run jest on single file 
Javascript :: get last item in array javascript 
Javascript :: javascript generate random numbers 
Javascript :: `useFindAndModify` is an invalid option. 
Javascript :: how remove last letter js 
Javascript :: start a react native project with type script 
Javascript :: set datetime-local value javascript 
Javascript :: how to install formik in react native 
Javascript :: react font awesome 
Javascript :: rxjs map 
Javascript :: node array 
Javascript :: how to reset input field in javascript 
Javascript :: select default value react 
Javascript :: js print all prime numbers 
Javascript :: how to get url in react 
Javascript :: js get first letter of string 
Javascript :: vscode react cannot find moudle when import image 
Javascript :: .children javascript 
Javascript :: loop through all dom elements javascript 
Javascript :: javascript clear input string 
Javascript :: set js 
Javascript :: js capitalize first letter 
Javascript :: javascript dynamic import 
Javascript :: nodejs binary string to decimal number 
Javascript :: get table row data jquery 
Javascript :: filter nested object array and return whole object 
Javascript :: javascript function loop through array 
Javascript :: postasync json C# 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =