vurcommon.blogg.se

Javascript case statement
Javascript case statement












javascript case statement

That is, if the switch expression matches the value you specified after the case keyword.

javascript case statement

Then, on the next line is the code you want to execute if the case is used. There are only colons at the end of the line. This can be a little bit confusing.Ĭase blocks don’t use curly braces. This keyword is then followed by some value, colons and code you want to execute if switch expression matches the value a case. Now, let’s talk about the case blocks.Ī case block consists of two parts. Second, it will compare this expression with values you specified for each statement. First, it will take the expression you passed in parentheses, that follow after the switch keyword. When you execute a switch statement, it will do two things. Every case block you add to a switch statement should have some value.Ī bit how it works. This, the case block, is also the third thing you need to make switch work. The first one is the expression you want to check. What JavaScript switch statement does is it operates with two parts. Switch statement uses different syntax, but the result is the same.

javascript case statement

Then, you can execute code specific for each boolean value, or one of them. In case of if.else, there is some condition and you “test” if that condition is either true or false. JavaScript switch statement works in a similar way to if.else statement. This block of code is the body of a switch statement. What follows next are curly braces with block of code. The expression goes between the parentheses that follow after the switch keyword. You will learn more about case blocks in “Case blocks” section. The second thing is an expression you want to compare with case value. Every switch statement has to start with this keyword. This tutorial will teach you what JavaScript switch statement is, how to use it and when.Įvery JavaScript switch statement must have three things in order to work. Switch statement can sometimes do a better job and make your code more readable. Switch statement is one of the oldest features of JavaScript.














Javascript case statement