Home  >  Article  >  Web Front-end  >  Analysis of javascript switch usage precautions_javascript skills

Analysis of javascript switch usage precautions_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:16:131482browse

This article analyzes the usage notes of switch in javascript with examples. Share it with everyone for your reference. The specific analysis is as follows:

Let’s look at the following code first:

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case '65':
alert("字符串65。jb51.net");
break;
}
</script>

You will find that no dialog box pops up and the alert is not executed.

Cause analysis:

What needs to be made clear here is that switch uses the congruent sign "===" when making judgments. When comparing congruent signs, it first checks whether the data types are the same, and here, t_jb51_net is of Number type. , and '65' is String.

The following code will pop up the dialog box:

<script>
var t_jb51_net = 65;
switch (t_jb51_net) {
case 65:
alert("数字65。jb51.net");
break;
}
</script>

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn