Break, continue, function cannot be overloaded in JavaScript

Source: Internet
Author: User

In JavaScript, break has a significant difference from continue.

If a break statement is encountered, the inner loop is terminated, regardless of the number of calculations that follow.

If a continue is encountered, only the loop is terminated, and the subsequent self-loop is still executed.

        varnum =0;  for(varK =0; K <3; k++) {             for(vari =0; I <3; i++) {                 for(varj =0; J <3; J + +) {                    if(i = = =1&& J = = =1) {                         Break; } num++; }}} console.log (num);

At this time num=3*3-3*2=21

If replaced with continue, then num=3*3-3*1=24;

See what happens when you switch to a label, the case code:

varnum =0; Comeouter: for(varK =0; K <3; k++) {             for(vari =0; I <3; i++) {                 for(varj =0; J <3; J + +) {                    if(i = = =1&& J = = =1) {                         BreakComeouter; } num++; }}} console.log (num);

Since it is a break, therefore, once i===1&j===1, then immediately exit all loops, so num=4;

If it is continue, only the outermost loop is retained, and the inner loop is not executed, so num=3*4=12;

Two

In an object-oriented language, we can overload a function depending on the parameter type of the function and the number of arguments, but there is no function overload in JavaScript.

If you define two JavaScript functions, the parser will only parse the one that is written at the bottom of the function.

        function TestA (NUM1, num2) {            Console.log (num1+'-'+num2);        };                function TestA (NUM1) {            console.log (NUM1);        };        TestA ();

The result of the run is 15.

May feel strange, I passed two parameters, call is a function with only one parameter, why not error?

This is because in JavaScript, the parser, regardless of the number of arguments passed in and the type, how much.

All of the parameters are actually placed in a arguments, which is similar to an array that can store different types of data and see the code below.

function TestA () {            console.log (arguments[0]+'--'+arguments[  1]);        };        TestA ();

The output result is 15--18.

Three

The difference between ' = = ' and ' = = = '

Take a look below

Console.log (true==1) Output true

Console.log (true===1) output false

Four

Any operation with Nan, the result of the output is Nan, such as Nan + 1.

Console.log (null ==undefined) output ture.

  

Break, continue, function cannot be overloaded in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.