te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Switch without break in Javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Switch without break in Javascript - Stack Overflow

programmeradmin3浏览0评论

drink = 'beer'

switch(drink){
    case 'beer':
    case 'whiskey':
        console.log('The drink is BEER or WHISKEY');
    case 'juice':
        console.log('The drink is JUICE');
    default:
        console.log('Nothing to drink');
}

drink = 'beer'

switch(drink){
    case 'beer':
    case 'whiskey':
        console.log('The drink is BEER or WHISKEY');
    case 'juice':
        console.log('The drink is JUICE');
    default:
        console.log('Nothing to drink');
}

For my code above, why do I get all three messages in the console? Can someone please explain? Without a break, I expected the case message and the default message to be printed, but why does the "juice" related message also appear in the console?

Share Improve this question edited Dec 27, 2023 at 12:59 Nick Parsons 50.9k6 gold badges57 silver badges75 bronze badges asked Feb 25, 2020 at 20:52 prabhprabh 3961 gold badge4 silver badges16 bronze badges 5
  • 7 Switch statements in JS 'fall through' cases that don't have a break. Quoting MDN: "If break is omitted, the program continues execution at the next statement in the switch statement." In your code, it matches the first case ('beer') and then 'falls through' each of the subsequent cases. – p.s.w.g Commented Feb 25, 2020 at 20:59
  • 1 check this developer.mozilla/it/docs/Web/JavaScript/Reference/… – Alberto Commented Feb 25, 2020 at 20:59
  • 1 @p.s.w.g i agree, but when the case 'juice': is not satisfied, still the console logs that message. Thats my question. Is it expected? – prabh Commented Feb 25, 2020 at 21:00
  • 2 @prabh thats what it means by fall thru. – Daniel A. White Commented Feb 25, 2020 at 21:01
  • 1 @DanielA.White sorry for my poor knowledge, i e from mainframe background and always coded in Natural programming language. We have Decide for first and decide for every condition statements there, But it always checks for condition before executing the logic. I thought it will fall thru the next Case to check if that condition is also matched and then only process the code. Didnt expect that Case will be ignored and just logic will be performed. – prabh Commented Feb 25, 2020 at 21:08
Add a ment  | 

4 Answers 4

Reset to default 10

According to the switch statement reference, "if you forget a break then the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met". So, inside a switch statement, once a case statement matches the given variable, all following statements will be executed ignoring the criteria until a break statement or closing curly bracket is encountered.

If you would rewrite your code to this, the output will only be "The drink is BEER or WHISKEY" and "Nothing to drink".

drink = 'beer'

switch(drink){
    case 'juice':
        console.log('The drink is JUICE');
    case 'beer':
    case 'whiskey':
        console.log('The drink is BEER or WHISKEY');
    default:
        console.log('Nothing to drink');
}

This is because of 'Fallthrough' (Read more here: https://en.wikipedia/wiki/Switch_statement#Fallthrough).

In some languages (like JavaScript and C) switch statements behave such that they will execute the matching block and then fallthrough to the statements that follow. This is why we need to break;, this will exit the switch block.

Play around with it to see the behavior. You are not breaking after the Beer case, so all blocks are being executed as Beer is the first case checked and it matches. If you change drink to Juice, only the last 2 blocks will be executed; the matching case, and the case that follows.

Switch cases in javascript "falls through" by default if no break or return statement is used.

For desired opearation you could write it like this:

drink = 'beer'

switch(drink){
    case 'beer':
    case 'whiskey':
        console.log('The drink is BEER or WHISKEY');
        break;
    case 'juice':
        console.log('The drink is JUICE');
        break;
    default:
        console.log('Nothing to drink');

}

You can use function

const res = ((val) => {
  if (['beer', 'whiskey'].includes(val)) return 'The drink is BEER or WHISKEY'
  if (val === 'juice') return 'The drink is JUICE'
  return 'Nothing to drink'
})(drink)

console.log(res)
发布评论

评论列表(0)

  1. 暂无评论