A summary of javascript Learning (basic knowledge) and a summary of javascript every day

Source: Internet
Author: User

A summary of javascript Learning (basic knowledge) and a summary of javascript every day

1. character conversion

Var s1 = "01"; var s2 = "1.1"; var s3 = "z"; // the 'z' letter cannot be converted to a number, therefore, NaN var B = false; var f = 1.1; var o = {valueOf: function () {return-1 ;}; s1 =-s1; // value becomes numeric-1 s2 =-s2; // value becomes numeric-1.1 s3 =-s3; // value becomes NaN B =-B; // value becomes numeric 0 f =-f; // change to-1.1 o =-o; // 1 first executes the valueOf () method of the object and returns-1, -- 1 is 1 o = + o; // the valueOf () value of-1 o is-1, +-1 or-1.

2. Special Character operations

Var result1 = 5-true; // 4 because true is converted to 1 var result2 = NaN-1; // NaN is not a number, naN var result3 = 5-3; // 2 var result4 = 5 -""; // 5 because "" is converted to 0 var result5 = 5-"2"; // 3 because "2" is converted to 2 var result6 = 5-null; // 5 because null is converted to 0

3. Variable-to-string operations

Var value1 = 10; var value2 = true; var value3 = null; var value4; // If value4 is not assigned a value, underfined is converted to 'underfined' alert (String (value1 )); // "10" alert (String (value2); // "true" alert (String (value3); // "null" alert (String (value4 )); // "undefined"

4. Digital hexadecimal conversion 

Var num = 10; alert (num. toString (); // "10" Default decimal alert (num. toString (2); // "1010" binary alert (num. toString (8); // "12" alert (num. toString (10); // "10" decimal alert (num. toString (16); // "a" hexadecimal

5. String comparison

Var result1 = 5> 3; // true var result2 = 5 <3; // false var result3 = "Brick" <"alphabet "; // true string comparison: lowercase letters are compared in alphabetical order after uppercase letters var result4 = "Brick ". toLowerCase () <"alphabet ". toLowerCase (); // compare var result5 = "23" <"3"; // true 2 to 3 var result6 = "23" <3; // false at this time, '23' is converted to 23 var result7 = "a" <3; // false because "a" becomes NaN character 'a' cannot be converted to a number var result8 = NaN <3; // false NaN and any number cannot be converted to a number, so it is always false var result9 = NaN> = 3; // false

6. Character Base Conversion

Var num1 = parseInt ("AF", 16); // 175 returns 10x16 + 15x1 var num2 = parseInt ("AF") in hexadecimal notation "); // The native mode is not specified for NaN. The native mode is 10 by default. Since AF is not in the decimal range, NaN alert (num1); alert (num2) is returned );

7. Use of parseInt

Var num1 = parseInt ("1234 blue"); // 1234 var num2 = parseInt (""); // The NaN character ''cannot be converted to the number var num3 = parseInt (" 0xA "); // A var num4 = parseInt (22.5) in hexadecimal notation of 10-hexadecimal ); // 22 var num5 = parseInt ("70"); // 70-decimal var num6 = parseInt ("0xf"); // 15 hexadecimal value: 15

8. Use of Number objects

Var num1 = Number ("Hello world! "); // NaN var num2 = Number (" "); // 0 empty strings can be converted to 0. The parseInt () is not the same as var num3 = Number (" 000011 "); // 11 var num4 = Number (true); // 1

9. NaN usage

  alert(NaN == NaN);  //false  alert(isNaN(NaN));  //true  alert(isNaN(10));  //false ?10 is a number  alert(isNaN("10"));  //false ?can be converted to number 10  alert(isNaN("blue")); //true ?cannot be converted to a number  alert(isNaN(true));  //false ?can be converted to number 1

10. Maximum number of systems

var result = Number.MAX_VALUE + 1;alert(isFinite(result)); // false

11. Infinity

  alert(5 * 6);   //30  alert(5 * NaN);   //NaN  alert(Infinity * 0); //NaN  alert(Infinity * 2); //Infinity  alert("5" * 5);   //25  alert(true * 10);  //10  alert(false * 10);  //0      alert(26 % 5); //1      alert(Infinity % 3); //NaN      alert(3 % 0); //NaN      alert(5 % Infinity); //5      alert(0 % 10); //0      alert(true % 25); //1      alert(3 % false); //NaN

12. for in Loop

for (var propName in window) {    document.write(propName);    document.write("<br />");  }

13. Special character comparison

 alert(null == undefined); //true  alert(null === undefined); //false    alert("NaN" == NaN);  //false  alert("NaN" === NaN);  //false  alert(NaN == NaN);   //false  alert(NaN === NaN);   //false  alert(NaN != NaN);   //true  alert(NaN !== NaN);   //true    alert(false == 0);   //true  alert(false === 0);   //false  alert(true == 1);   //true  alert(true === 1);   //false    alert(null == 0);   //false  alert(undefined == 0);  //false    alert(5 == "5");   //true  alert(5 === "5");   //false  

14. Boolean object

 var message = "Hello world!";  var messageAsBoolean = Boolean(message);    alert(messageAsBoolean); //true

15. Use the for loop and tag together
Both the break statement and the continue statement can be used together with the labeled statement to return a specific position in the code.
This is usually done when there is a loop inside the loop, for example:

Var num = 0; // return outermost: for (var I = 0; I <10; I ++) {for (var j = 0; j <10; j ++) {if (I = 5 & j = 5) {break outermost;} num ++ ;}} alert (num ); // 55

In the preceding example, the tag outermost indicates the first for statement. Under normal circumstances, each for statement executes 10 code blocks, which means that num ++ will be executed 100 times under normal circumstances. When the execution is complete, num should be equal to 100. Here, the break statement has a parameter, that is, the label of the statement to jump to after the loop is stopped. In this way, the break statement can not only jump out of the internal for statement (that is, the statement using the variable j), but also jump out of the external for statement (that is, the statement using the variable I ). Therefore, the final value of num is 55, because when the values of I and j are both 5, the loop ends.
You can use the continue statement in the same way:

Var iNum = 0; outermost: for (var I = 0; I <10; I ++) {for (var j = 0; j <10; j ++) {if (I = 5 & j = 5) {continue outermost;} iNum ++ ;}} alert (iNum); // output "95"

In the above example, the continue statement forces the loop to continue, not only the internal loop, but also the External Loop. This occurs when j is equal to 5, which means that the internal loop will be reduced by 5 iterations, resulting in the iNum value being 95.

Tip:It can be seen that the tag statements used in combination with break and continue are very powerful, but over-using them will cause trouble for debugging code. Make sure that the labels used are descriptive and do not nest too many layers of loops.

The above is the summary of today's javascript learning, and will be updated every day. I hope you will continue to pay attention to 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.