How to assign a value only if the variable is null or undefined or 0 in JavaScript

2 Answers

0 votes
let a = 44;
a ||= 10;
console.log(a);

let b;
b ||= 10;
console.log(b);

let c = null;
c ||= 10;
console.log(c);

let d = 0;
d ||= 10;
console.log(d);

 
/* 
run:
 
44
10
10
10
 
*/

 



answered Oct 31, 2024 by avibootz
0 votes
let a = 44;
a ??= 10;
console.log(a);
 
let b;
b ??= 10;
console.log(b);
 
let c = null;
c ??= 10;
console.log(c);
 
let d = 0;
d ??= 10;
console.log(d);
 
  
  
/* 
run:
  
44
10
10
0
  
*/

 



answered Nov 1, 2024 by avibootz
...