export const setID = (v) => {
console.log('执行setID',v);
let l = v.length;
switch(l)
{
case l < 6 :
console.log('qq');
break;
default:
console.log('11111');
}
}
v
is the incoming string. The first console
of this code is executed normally, and the second console
is not executed under any circumstances. Now I am sure that there is something wrong with my switch...case...
. Please tell me what the problem is
Change it to the following
But you can definitely use an if statement:
According to the original writing, it should be whether
l
andl < 6
are equal.l
is an integer, indicating the length of the string.l<6
is a Boolean value. Integers and Boolean values are not congruent. , so thedefault
statement will always be used;The misunderstanding of the original writing method: it is not that the
case
statement will be executed if it is true, but the content in the switch expressionl
and the content after the case statementl< Matches only when 6
are congruent; assumingv="111"
, thenl=3
l<6
is true, but3!==true
, so the default statement is used.Are you sure your l is less than 6?