text JS基础知识 - 笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text JS基础知识 - 笔记相关的知识,希望对你有一定的参考价值。

JS Basisc - Notes

### Type Conversions ###

To String conversion.

  String conerstion happen when wee need to display a value in the string.
  For instance:

  let x = 2;
  alert(x);  // display 2 as a string

  Alert function converts value to the string.

To Number conversion
  Numeric conversion happens in mathematical functions and expressions automatically.
  For example 
  
  alert("6" / "2"); // 3 
  // in this case both strings were converted to the numbers 
  
  the same happens with multiplying
  
  alert("6" * "2"); // 12
  
  --------
  
  Another way to conver string to number is by using funciton:
  Number(value);
  
  example:
  let str = "123";
  alert(str); // "123" displays string
  alert(typeof str); // string
  
  let num = Number(str); // 123
  alert(num); // 123 displays number
  alert(typeof num);  // number
  
  --------
  
  So how does the Number(value) function work?
  
  The function read the string and expects to see there only string which cintains numbers. 
  If the readed variable has letters the function will return NaN.
  Number() has a duild in protection which deleates the spaces from between characters.
  Few of examples:
  
  Example One
  
   let str = "1 two 3";
  let num = Number(str); 
  alert(num); // NaN
    // Function Number() will deleate the white spaces from the string -> "1two3"
    // and then will check for the numbers inside. Between 1 and 3 are letters so the function
    // will return NaN - Not a Number.
 
  Example Two
  alret(Number (" 123 ")); // 123
    // function Number() deleated the white spaces from the begining and the end 
    // and then it checked for the number
    // it has found a number 123 in the string so it has converted it to the number
    
  Example Three  
  alret(Number (" 1 2 3 ")); // NaN
    // If there will be white space between digits function will return NaN
    
  Example Three
  alret(Number("one two three"); // NaN
    // Function returned Nan because the strings is made from letters
    
  ------
  
  Value	Becomes…
    undefined         ->	NaN
    null	            ->   0
    true and false	  ->  1 and 0
    string	          ->  Whitespaces from the start and the end are removed. 
                          Then, if the remaining string is empty, the result is 0. 
                          Otherwise, the number is “read” from the string. 
                          An error gives NaN.
  
  
To Boolean
  Boolean conversion is the simplest one.
  It happens in logical operations, but also can be performed manually with the call of Boolean(value).

  The conversion rule:

  Values that are intuitively “empty”, like 0, an empty string, null, undefined and NaN become false.
  Other values become true.
  
  alert( Boolean(1) ); // true
  alert( Boolean(0) ); // false
  alert( Boolean("hello") ); // true
  alert( Boolean("") ); // false
  alert( Boolean("0") ); // true
  alert( Boolean(" ") ); // spaces, also true (any non-empty string is true)

Summary

  "" + 1 + 0 = "10" // In this case the addition work from left to right
  // first the string is compared to the number. number is coverted to the string 
  // and added to the first string: "" + 1 = "1"
  // Then string "1" is compared to the number 0 and the number is coverted to the string 
  // and added to the previous string: "1" + 0 = "10"
  
  -----
  
  "" - 1 + 0 = -1 // The subtraction - (like most math operations) only works with numbers, 
  // it converts an empty string "" to 0.
  
  -----
  
  true + false = 1
  
  -----
  
  6 / "3" = 2 // number divided by string. String is coverted to the number
  
  -----
  
  "2" * "3" = 6 // String multiplied by string. Both strings are coverted to the numbers and then multiplied
  
  -----
  
  4 + 5 + "px" = "9px" // Reading from left side -> number + number + string
    // 4 + 5 = 9 <- number + number = number
    // 9 + "px" = "9px" <- number + string = string
  
  -----
  
  "$" + 4 + 5 = "$45" // "$" + 4 = "$4" <- string + number = string
    // "$4" + 5 = "$45" <- string + number = string
  
  -----
  
  "4" - 2 = 2  // In this case string will be converted to the number and operation will be excecuted
    // string - number = number
    
  -----
  
  "4px" - 2 = NaN // String which contains number and letters wont be converted to the number
    // therefor the result of this substraction is NaN <- Not a Number
  
  -----
  
  7 / 0 = Infinity // number divided by 0 is infinity 
  
  -----
  
  " -9\n" + 5 = " -9\n5" // this equasion is going to be solved ion the way:
    // string + number = string
    
  -----
  
  " -9\n" - 5 = -14 // in this case the empty space will be deleted from the begining
    // of the string " -9\n" and also character \n will be deleted as well. This set of character is
    // treated as a new line command which is considered a white space. It will be treated as:
    // Number(" -9\n") - 5 = -14 <-  number - number = number
  
  -----
  
  null + 1 = 1 // null is treated as zero therefore the equasion will look like that:
    // 0 + 1 = 1 <- number + number = number 
  
  -----
  
  undefined + 1 = NaN // undefined becomes NaN after the numeric conversion.
  

### Logical operators ###
  
OR
  
    alert( true || true );   // true
    alert( false || true );  // true
    alert( true || false );  // true
    alert( false || false ); // false
  
    || OR seeks the first truthy value if in the chain bigger than two conditions
  
    result = value1 || value2 || value3;
      The OR || operator does the following:
      Evaluate operands from left to right.   
      For each operand, convert it to boolean. If the result is true, 
      then stop and return the original value of that operand.
      If all other operands have been assessed (i.e. all were false), return the last operand.
    
AND

    alert( true && true );   // true
    alert( false && true );  // false
    alert( true && false );  // false
    alert( false && false ); // false
    
    AND seeks the first falsy value
    
    Given multiple AND’ed values:

    result = value1 && value2 && value3;
    
    Evaluate operands from left to right.
    For each operand, convert it to a boolean. 
    If the result is false, stop and return the original value of that operand.
    If all other operands have been assessed (i.e. all were truthy), return the last operand.
    
!NOT

    - Converts the operand to boolean type: true/false.
    - Returns an inverse value.
    
    A double NOT !! is sometimes used for converting a value to boolean type.

以上是关于text JS基础知识 - 笔记的主要内容,如果未能解决你的问题,请参考以下文章

js基础教程学习笔记

2016年5月27日下午(妙味课堂js基础-3笔记三(事件))

javaweb实训第二天上午——jQuery笔记

javaweb实训第二天上午——jQuery笔记

Jquery的三章的笔记

js-权威指南学习笔记15