JS Note 2--Amount, is the scope of the pit ~

Source: Internet
Author: User

<script>function Calculate (a,c,b) {if (c== ' + ') {var d=a+b;} if (c== '-') {var d=a-b;} if (c== ' * ') {var d=a*b;} if (c== '/') {var d=a/b;} return D;} Calculate (2, ' * ', 6);     Cannot output result//alert (d);   Alert (Calculate (2, ' * ', 6));  Can output normally </script>

The problem is listed, why?

OK, is the scope of the problem, is the scope of the problem, the function is declared with Var, is a local variable ~ ~ ~ minus the var can be normal output, but it is best not to do so, you can not assign the result to another variable:

<Script>    functionCalculate (a,c,b) {if(c=='+'){returna+b;} if(c=='-'){returna-b;} if(c=='*'){returna*b;} if(c=='/'){returna/b;}} alert (Calculate (2,'*',6)); </Script>

I know this mistake is too low, but, I really did not find it at the beginning! Remember to remember!


JS Note 2--Amount, is the scope of the pit ~

Related Article

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.