Can you nest if, else if, and if statements in JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can you nest if, else if, and if statements in JavaScript?

17th Sep 2018, 6:11 AM
Ethan Morrell
Ethan Morrell - avatar
11 Answers
+ 8
var x= 17 if( x>0){ if(x>10){ ..... }else{ if(x==9) .... } }else{ .... }
17th Sep 2018, 7:54 AM
KrOW
KrOW - avatar
+ 7
Checking a value from input and write the result on the screen with javascript (else if). Try it out: https://code.sololearn.com/Wvobd0cYj1Em/?ref=app Note: here is a similar code like what Wagner D'Amaral posted - just mine is work. 🙃😁
13th Oct 2018, 6:39 AM
Donthack
Donthack - avatar
+ 3
Yes
17th Sep 2018, 7:49 AM
KrOW
KrOW - avatar
+ 3
Yes, we can.
25th Sep 2018, 8:12 AM
Codeie
Codeie - avatar
+ 2
Yes , we can nest as many as if statements in our program... Here, take a look.. {I have taken an example from a c++ program... but you can use it in the same way in JavaScript ;) } if (mark >= 50) { cout << "You passed." << endl; if (mark == 100) { cout <<"Perfect!" << endl; } } else { cout << "You failed." << endl; }
20th Sep 2018, 4:45 PM
Vedic Bhatnagar
+ 1
KrOW Can you give me an example? I tried to do it but I was unsuccessful.
17th Sep 2018, 7:50 AM
Ethan Morrell
Ethan Morrell - avatar
+ 1
Ethan, try this code. if you have question, please ask. var x =100; if(x < 25) { document.write('Less than 25'); } else if (x < 50) { document.write('Less than 50); else if (x < 100) { document.write('Less than 100'); else { if (x === 100) { document.write('Equals 100'); else { document.write('Greater than 100'); } }
17th Sep 2018, 11:57 AM
Wagner D'Amaral
Wagner D'Amaral - avatar
+ 1
Ofcourse.
17th Sep 2018, 12:15 PM
Joshua Martin Fontanilla
Joshua Martin Fontanilla - avatar
+ 1
Obviously yes :) . Good question though. Keep learning!
26th Sep 2018, 1:07 PM
Darren Moody
Darren Moody - avatar
0
Yeeep
20th Sep 2018, 7:29 AM
Nibir Paran Barman
Nibir Paran Barman - avatar
- 3
js
22nd Sep 2018, 9:36 AM
Abhishek Paswan (SPIC)
Abhishek Paswan (SPIC) - avatar