28
Calling a function only if a condition is true
2021-03-03 - JavaScript
1// JS Tip of the Day
2// Calling a function only if a condition is true
3
4const myFavouriteHero = "Batman";
5const callHero = (hero) => console.log(`Help me ${hero}!`);
6
7
8// Longhand
9if (myFavouriteHero) {
10 callHero(myFavouriteHero); // Help me Batman!
11}
12// Shorthand
13myFavouriteHero && callHero(myFavouriteHero); // Help me Batman!
#JStipoftheday