Some JavaScript knowledge that you may not know (but it's useful after you know it)

Source: Internet
Author: User
Tags script tag

1 、!! Easily and quickly convert a value to a Boolean value
Console.log (!! window===true );
2, do not declare the third variable to achieve the Exchange
var a=1,b=2; a=[b,b=a][0]; // after executing this code, the value of a is 2 B and the value is 1.
3, && and | | The use of (learned to immediately feel tall still)

Say it in code.

 var  day= (new  Date). GetDay () ===0;  //  traditional if statement  if   ' Today is sunday! ')  //  Day&&alert (' Today is sunday! '); //   
        if (! a) {            = {};        }         // The above three words can be replaced        with the following sentence // explain this sentence.   If there is a declaration of this variable, then it is the original object.  If you don't, create an object for it .
4, Nan is not equal to any value, including its own

We all know that in any programming language, if the divisor is 0 will be an error, and in JS does not, it returns the value of Nan,nan represents a result of the operation can not produce normal results, we can use isNaN (number) to detect is not Nan, But you may not know that Nan is not equal to any value, including itself.

5. Use the script tag to save any information

Setting the script tag to type= ' text ' can then store any information inside it, which can then be easily retrieved in JavaScript code.

The page code looks like this:

    <script type= "text" id= "Angelascript" >    

The results appear as shown:

6, JS Some of the use of switch

Look at the effect of the operation, summed up a sentence Oh!

    • 1) The comparison between the switch parameter variable and the case is identical, that is to say = = = Comparison
    • 2) Not every case is required to write a break, if not written, it will be executed in sequence.
    • 3) The switch statement can not only use numbers, characters to make conditional expressions, but also use strings!

Look at the use of the switch below, you instantly feel that switch is so powerful!

7, understand the scope, clearly understand the current this is who

8, you may not know the JS array
    • The length property of an array is not enumerable
    • The array of JS can be accessed by a numeric subscript in the form of a string, because the elements of the array are actually properties of the array object.
    • Delete can be used to remove elements from an array, but because an element is an attribute, delete leaves a "hole" in the array; the element deleted by delete is undefined, and using the splice method may be a better approach. For example, to delete the 3rd element of the ARR array, you can use the statement: Arr.splice (2,1), but for large arrays, the function may not be efficient.

9, HTML set the ID of the element is actually JS global variables Oh

The HTML page code looks like this

    <div id= "Angeladiv" >    </div>    <script type= "Text/javascript" >        console.log ( ANGELADIV);    </script>

The results that you see in the Chrome console are as shown in

10, the string can actually use subscript access

We all know that the string is immutable, and so is the case in JS. This means that once a string is created, it cannot be changed. But I believe very few people know that in JS can use subscript to access a character in a string, although you can use subscript read but can't modify Oh (although the modification operation will not error, but it does not work)

11. Some methods of string
    • The substring function of string has the same usage as slice, but it cannot handle negative arguments. The Substr method is not standardized in ECMAScript, so if you want to take a substring of a string, use the slice Method!
    • The split (Separator,limit) method of string splits a string into fragments to create an array of strings. The optional parameter limit limits the number of fragments to be split. The separator parameter can be a string or a regular expression, and the text from the grouped capture will be included in the segmented array
var s = ' str1, str2, Str3 '; S.split (/\s (,) \s/,4);   // output results: ["str1", ",", "str2", ","]  

12, the browser as an editor

Enter the following code in the browser address bar, immediately turn the browser into a notepad, bad!

<contenteditable>
13, Vertical center

We all know that CSS inside with Text-align:center plus margin:0 Auto can be achieved horizontally centered, but the vertical center has no corresponding CSS properties to set, and if you want to set the element can be vertically centered must be set to the container display:table , and then set the child element, which is the element to be vertically centered, to Display:table-cell, and then add vertical-align:middle to implement it.

Look at the code! The following code uses CSS3 's transform to achieve vertical centering and horizontal centering

{    position: relative;     top: 50%;     transform: translatey ( -50%);}  {    position: relative;     left: 50%;     transform: TranslateX ( -50%);}

Some JavaScript knowledge that you may not know (but it's useful after you know it)

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.