Daily javascript Summary (Basic Knowledge)

  • 2020-10-23 19:59:54
  • OfStack

1. Character conversion


 var s1 = "01";
  var s2 = "1.1";
  var s3 = "z";// The letter 'z' Cannot convert to number, so or return 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  Execute the object first valueOf() Method returns -1,--1 for 1
    o = +o;//-1 o the valueOf() A value of -1 . +-1 or -1

2. Special character operation


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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 string operation


 var value1 = 10;
  var value2 = true;
  var value3 = null;
  var value4;//value4  No assignment is underfined  Converting to a string is 'underfined'
  
  alert(String(value1));  //"10"
  alert(String(value2));  //"true"
  alert(String(value3));  //"null"
  alert(String(value4));  //"undefined"

4. Decimal conversion of Numbers


var num = 10;
  alert(num.toString());  //"10" The default 10 Into the system 
  alert(num.toString(2));  //"1010"2 Into the system 
  alert(num.toString(8));  //"12"8 Into the system 
  alert(num.toString(10));  //"10"10 Into the system 
  alert(num.toString(16));  //"a"106 Into the system 

5, string comparison operation


var result1 = 5 > 3; //true
  var result2 = 5 < 3; //false
  var result3 = "Brick" < "alphabet"; //true  String comparisons are performed alphabetically   Lowercase letters follow uppercase letters 
  var result4 = "Brick".toLowerCase() < "alphabet".toLowerCase(); //false Alphabetical comparison 
  var result5 = "23" < "3"; //true  character 2 Less than the characters 3
  var result6 = "23" < 3; //false  At this time '23' Converted to 23 the 
  var result7 = "a" < 3; //false because "a" becomes NaN  character 'a' It cannot be converted to a number 
  var result8 = NaN < 3; //false NaN  Compared to any number, it can't be converted into a number, so 1 Straight is false
  var result9 = NaN >= 3; //false

6. Character conversion


  var num1 = parseInt("AF", 16);  //175  In accordance with the 16 Hexadecimal output 10 Hexadecimal data  10*16+15*1
  var num2 = parseInt("AF");   //NaN  No base specified, default to 10 Base conversion, because AF Not in 10 Base range, return NaN

  alert(num1);
  alert(num2);

7. Use of parseInt


  var num1 = parseInt("1234blue"); //1234
  var num2 = parseInt("");   //NaN  character '' It cannot be converted to a number 
   var num3 = parseInt("0xA");   //10 - hexadecimal 16 Into the system A
  var num4 = parseInt(22.5);   //22
  var num5 = parseInt("70");   //70 - decimal
  var num6 = parseInt("0xf");   //15 16 Into the system for 15

8. Use of Number objects


  var num1 = Number("Hello world!"); //NaN
  var num2 = Number("");    //0  An empty string can be converted to 0  this parseInt() Don't 1 sample 
   var num3 = Number("000011");  //11
  var num4 = Number(true);   //1

9. NaN


  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 system


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

11. Infinity is infinite


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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
0

12. for in cycle


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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
1

13. Special character comparison


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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
2

14. Boolean object


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

15. for cycle and label 1 start to be used
Both break and continue statements can be used in conjunction with tagged statements to return specific locations in code.
Usually, this is done when there is a loop inside the loop, for example:


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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
4

In the above example, the tag outermost represents the first for statement. Normally, each for statement executes a block of code 10 times, which means that num++ would normally be executed 100 times, and num should be equal to 100 when execution is complete. The break statement here has one argument, the label of the statement to jump to after the loop is stopped. In this way, the break statement can jump out not only the internal for statement (that is, the statement using the variable j), but also the external for statement (that is, the statement using the variable i). Therefore, the final value of num is 55, because the loop terminates when both i and j are equal to 5.
You can use the continue statement in the same way:


 var result1 = 5 - true; //4 because true is converted to 1
  var result2 = NaN - 1;  //NaN NaN not 1 A number, and any number to do any arithmetic 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
5

In the above example, the continue statement forces the loop to continue, not only the inner loop, but also the outer loop. This occurs when j is equal to 5, which means that the inner loop will be reduced by five iterations, resulting in a value of 95 for iNum.

Tip: As you can see, the tagged statements used in conjunction with break and continue are very powerful, but overusing them can cause problems in debugging code. Be sure to use declarative tags without nesting too many layers of loops.

So that's the summary of today's javascript, and we will continue to update it every day. I hope you will continue to pay attention to it.


Related articles: