Lear more about JavaScript If condition through this small JavaScript article. Read this short JavaScript tutorial, practice yourself with some tiny JavaScript If examples to understand and use JavaScript if clause smarter and better.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Just a quick post, inspired by Laura Kalbag's post, which included this gem:
We shouldn't be fearful of writing about what we know. Even if you write from the most basic point of view, about something which has been 'around for ages', you'll likely be saying something new to someone.
One: There is no else if
When you write something like this ...
function saySomething( msg ) { if ( msg === 'Hello' ) { console.log('Hello there'); } else if ( msg === 'Yo' ) { console.log('Yo dawg'); } }
... then what you're actually writing is this ...
function saySomething( msg ) { if ( msg === 'Hello' ) { console.log('Hello there'); } else { if ( msg === 'Yo' ) { console.log('Yo dawg'); } } }
That's because there is no else if
in JavaScript. You know how you can write an if
statement without any curly braces?
if ( foo ) bar() // please don't do this if you want your code to be legible
You're doing the same thing with the else
part of the initial if
statement when you write else if
: you're skipping the curly braces for the second if
block, the one you're providing to else
. There's nothing wrong with else if
per se, but it's worth knowing about what's actually happening.
Two: return
Means Never Having to Say else
Consider some code like this:
function howBig( num ) { if ( num < 10 ) { return 'small'; } else if ( num >= 10 && num < 100 ) { return 'medium'; } else if ( num >= 100 ) { return 'big'; } }
If the number we pass to howBig
is less than 10, then our function will return 'small'
. As soon as it returns, none of the rest of the function will run – this means we can skip the else
part entirely, which means our JavaScript code could look like this:
function howBig( num ) { if ( num < 10 ) { return 'small'; } if ( num < 100 ) { return 'medium'; } if ( num >= 100 ) { return 'big'; } }
But wait – if the first if
statement isn't true, and the second if
statement isn't true, then we will always return 'big'
. That means the third if
statement isn't even required:
function howBig( num ) { if ( num < 10 ) { return 'small'; } if ( num < 100 ) { return 'medium'; } return 'big'; }
From
Link: http://rmurphey.com/blog/2012/12/10/js-conditionals- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k