Today I learnt the use of the conditional if/else if statements and the query selector in JavaScript and created a blackjack app. Blackjack is a game that you win when your card summation is nearer or equals to 21. 21 is the golden summation, but if there isn’t anyone in the game that has the number, the nearest number to 21 wins the game.
Conditional statements control behavior and determine whether or not pieces of code can be executed following an instruction. The types of conditional statements are:
• The ‘if’ statement
• ‘Else if’ statement
• ‘ else’ statement
The if statement is where if a condition is true, the blocks of code/statement will be executed. The else if statement is when the first condition is false then this will be executed. Then the else statement is where if all the statements preceding this statement are false then it will be executed.
Example
let firstNumber = 6
let secondNumber =13
let sum = firstNumber + secondNumber
if (sum < 21) {
console.log(“ You could be the winner”)
}
Else if example
if (sum < 21) {
console.log(“ You could be the winner”)
}
else if ( sum ===21) {
console.log(“ Congratulations you have won the blackjack game” )
}
Else example
f (sum < 21) {
console.log(“ You could be the winner”)
}
else if ( sum ===21) {
console.log(“ Congratulations you have won the blackjack game” )
}
else{
console.log (“ Sorry better luck next time”)
}
Other two things I also learnt were the "==" and the "===".
The difference between them.
Example
5 =='5'
This will return true as it sees it as being similar irrespective of the data type difference. Hence you would say. It is not strict in differentiating.
5==='5'
This will return false as there are two different datatypes even though they look similar in view. The first is number five while the second is a string data type.
Top comments (9)
Try to elaborate more in the blog.. That would make more sense...
BTW. recently I've also published my article about If-else and swicth..
You can check out here:
if else in JavaScript - Conditional Statements
Switch statement in JavaScript
Thanks for the input. I will do do.
Keep up the good progress!
Thank you
Keep learning 👏👏👏
Great start 🎉
Thank you.
Hello, which course are you following?
I enrolled in the Scrimba JavaScript course.