Example 1: Simple Program Using switch...case Suppose we want to display a message based on the current day of the week. Let's look at the example below to see how we can achieve this usingswitch...case. letday =3;letactivity;switch(day) {case1:console.log("Sunday");break;case2:...
You have to use thelogical OR, AND operatorsin the switch case to use multiple values in JavaScript. JavaScript switch case multiple values Simple example code test multiple conditions in a JavaScript switch statement. Here is theOR condition, anyone true will execute the case block. <!DOCTYPE h...
document.body.onclick=function(event){alert(event.currentTarget===document.body);//truealert(this===document.body);//truealert(event.target===document.getElementById("myBtn"));//true}; 通过event.type与switch case组合,可以通过一个函数处理多个事件。 只有在事件处理程序执行期间,event对象才会存在;...
https://www.freecodecamp.org/news/javascript-switch-case-js-switch-statement-example/ Activity Dario-DCadded italian on Apr 15, 2024 Dario-DC commented on Apr 15, 2024 Dario-DCon Apr 15, 2024 ContributorAuthor menzionato alla fine di https://www.freecodecamp.org/italian/news/ghost/#/...
switch(newDate().getDay()) { case6: text ="Today is Saturday"; break; case0: text ="Today is Sunday"; break; default: text ="Looking forward to the Weekend"; } Try it Yourself » Example Sometimes you will want different cases to use the same code, or fall-through to a common...
The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed.Example The getDay() method returns the weekday as a number between 0 and 6. (Sunday=0, Monday=1, Tuesday=2 ...
Like an example shown above, we can use the switch statement for multiple cases making our work easier. c. Using switch to find the type let a = 5; switch (a) { case 1: a = 5; break; case 5: a = 'five'; break; case 3: a = 'V'; break; case “four”: a = 'FIVE';...
JavaScript Switch Statement Example <!-- your JavaScript goes here --> <!-- try changing the data in "value" and run --> var value=2; var message=''; switch(value){ case 1: message += "Value is 1."; break; case 2: message += "Value is 2."...
self.addEventListener('message',function(e){vardata=e.data;switch(data.cmd){case'average':varresult=calculateAverage(data);// 从数值数组中计算平均值的函数self.postMessage(result);break;default:self.postMessage('Unknown command');}},false); ...
(5)switch语句; 二、循环结构 在JavaScript中,循环结构总有3种: (1)while语句; (2)do……while语句; (3)for语句; 三、跳转语句 JavaScript支持的跳转语句主要有2种: (1)break语句; (2)continue语句; break语句与continue语句的主要区别是:break是彻底结束循环,而continue是结束本次循环。在这一点跟其他编程...