![]() ![]() If that is the case, the function must print out using console.log the word correct. ![]() In this exercise, you must construct an if inside the checkNumber function statement that checks if the number myNumber is equal to 42. Using the switch statement in general is not recommended, because forgetting the break keyword causes very confusing results. We must use the break statement between every code block to avoid the switch from executing the next code block. If an unknown rank was evaulated, the default keyword defines the action for this case (optional). In this example, "Private" and "Sergeant" both trigger the first sentence, "Commander" triggers the second sentence and "Captain" triggers the third. For example: var rank = "Commander" Ĭonsole.log("Hello commander! what can I do for you today?") Ĭonsole.log("Hello captain! I will do anything you wish.") Ĭonsole.log("I don't know what your rank is.") The switch statement is used to select between more than two different options, and to run the same code for more than one option. The switch statement is similar to the switch statement from the C programming language, but also supports strings. The NOT operator ! can also be used likewise: var notTrue = false For example: var foo = 1 Ĭonsole.log("foo is smaller than bar AND moo is larger than bar.") Ĭonsole.log("foo is smaller than bar OR moo is larger than bar.") ![]() ![]() Inequality operators can also be used to evaluate expressions. This means that it will be true only if the two things you are comparing are the same type as well as same content. There is also another equality operator in JavaScript, =, which does a strict comparison. To evaluate whether two variables are equal, the = operator is used. It is also possible to omit the else keyword if we only want to execute a block of code only if a certain expression is true. The if statement allows us to check if an expression is equal to true or false, and execute different code according to the result.įor example, if we want to ask the user whether his name is "John", we can use the confirm function. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |