Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,684 questions

55,436 answers

573 users

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
...