Several values in JavaScript that can be converted to false

Source: Internet
Author: User

1, [0,nan, "", null,undefined] can be converted directly to false, but these values are not exactly equal

1     vararr = [0, "",false,NULL, Undefined,nan]2      for(vari=0;i<arr.length;i++){3          for(varj=i;j<arr.length;j++){4             if(arr[i]==Arr[j]) {5Console.log (arr[i]+ "=" +arr[j]);6             }7         }8         if(arr[i]!==Arr[i]) {9Console.log (arr[i]+ "! =" +arr[i]);Ten         } One}

2. Variable declaration method

 Defines multiple variables separated by a "," number, where the use of property values inside an object is similar to when the object is defined by a literal.

But the variable is assigned the name = "Zhang San";

The object property is assigned the name: "Zhang San";

<script>    var num = 123,        = "Kyle",        //Name This named variable is not recommended for properties of the top-level object windows, and the name data type is always string        true;    Console.log (Num,result,result); </script>

3. If statement does not add curly braces {}

If the condition is met, the code after the if is executed by default. It is recommended to add curly braces

4. How to change the character specified in a string

A simple alternative to regular expression

1     var str = "ADWGWHFAJKTEJSDAAASQ4SAADDDQWESDAAFDF"; 2     Console.log (Str.replace (/aa/g, "-")); 3     Console.log (Str.split ("AA"). Join ("-"));  
Splits a string as "AA" into an array, with "-" concatenated arrays as strings

5, the Math object--math.randon () probability demonstration;

Math.Round () and Math.floor () demo

1     vararr = [0, 1, 2, 3, 4, 5];2     functionRandom (value1,value2,value3,value4,value5) {3          This[1] =value1;4          This[2] =value2;5          This[3] =Value3;6          This[4] =value4;7          This[5] =Value5;8     }9     varo =NewRandom (0,0,0,0,0);Ten     varO1 =NewRandom (0,0,0,0,0); One     varj = 100000; A      for(vari = 0; I < J; i++) { -          for(varKeyincho) { -Math.Round (Math.random () * 5) = = key? o[key]++: {};//Rounding the         } -          for(varKeyinchO1) { -Math.floor (Math.random () * 6) = = key? o1[key]++: {};//is rounded down -         } +     } -Console.log (o, O1);

6. When creating an object, use a pure number as the attribute name considerations

The constructor calls this[numbers from the creation of this[numbers];

Literal creation Number: Call this[number];

Several values in JavaScript that can be converted to false

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.