JavaScript operators apply

Source: Internet
Author: User

What does the following code output? Why?

Console.log (1 + "2" + "2"), Console.log (1 + + "2" + "2"), Console.log (1 +-"1" + "2"), Console.log (+ "1" + "1" + "2"); CONSOLE.L OG ("A"-"B" + "2"); Console.log ("A"-"B" + 2);

The results were

122,32,02,112,nan2,nan

The second and third questions are to be noted here.

The second question of the + "2" result is to convert the string 2 into the number 2, the result is a string 32

The third question-the result of "2" is to convert the string 2 into the number 2, resulting in a string of 02

The +,-operator acts as a transform here, converting a string to a computable expression, which is useful for writing self-executing functions.

(function () {}) ()  //Return Undefined+function () {} ()   //Return Nan-function () {} ()   //Return Nan!function () {} ()   //Return true~ function () {} ()   //Return-1

These can act as self-executing, but the return value is different.

JavaScript operators apply

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.