Let's look at the example below to see how we can achieve this using switch...case. let day = 3; let activity; switch (day) { case 1: console.log("Sunday"); break; case 2: console.log("Monday"); break; case 3: console.log("Tuesday"); break; case 4: console.log("Wednesda...
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对象才会存在;...
/表达式/[switch] switch(也称为修饰符) 按照什么样的模式来匹配. 有三种值: g:全局匹配 i:忽略大小写 gi:全局匹配 + 忽略大小写 案例:敏感词过滤 代码语言:javascript 代码运行次数:0 运行 AI代码解释 <textarea name="" id="message" cols="30" rows="10"></textarea> 提交 var text = document...
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/#/...
case 'brinjal': case 'potato': default: console.log('This is not a fruit.'); } When you run the above-written code, you will get the output as shown below: >“This is a fruit.” Like an example shown above, we can use the switch statement for multiple cases making our work easi...
Example switch(newDate().getDay()) { default: text ="Looking forward to the Weekend"; break; case6: text ="Today is Saturday"; break; case0: text ="Today is Sunday"; } Try it Yourself » Ifdefaultis not the last case in the switch block, remember to end the default case with...
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 ...
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); ...
The if statementThe else statementThe else if statementRandom linkSwitch statement Conditionals Explained JavaScript Loops For loopLooping an ArrayLooping through HTML headersWhile loopDo While loopBreak a loopBreak and continue a loopUse a for...in statement to loop through the elements of an objec...