JavaScript implicit type conversion and display knowledge and summarize && and || rules

basic knowledge

By switching between the built-in functions type (native function) is achieved:
1. String ()
2. Number The ()
3. Boolean ()

Note : There is no new keyword in front;


Abstract rules of operation:

  • toString rule : other types of display conversion rule string
    JOSN.stringify () in the conversion rule and toString strings, numbers, Boolean values, and substantially the same as null.

    Type conversion rules for the basic values:
    null ----> "null"
    undefined -> "undefined"
    to true ----> "to true"

Ordinary objects, unless the self-defined otherwise toString () (Object.prototype.toString ()) Returns the internal property [[Class]] value, such as String ({}) to give "[object Object]", where the first a fixed object, the second object is Object shows the internal [[Class]] value of the property.

  • toNumber rules non-digital converted into digital rules:
    can be converted to the corresponding type of time base converter 1. Number object, call built valueOf () or toString () method returns the value obtained for cast. First call valueOf if not return to the basic type value, calling toString, or if there is no return to the basic type value, resulting in a TypeError.
    2. Note that no toNumber method
    3. The following are common to other types of rules are converted to digital:

    Number(true) //1
    Number(false) // 0
    Number(undefined) //NaN
    Number(null) // 0  
    
    Number({ })//NaN
    Number([])//NaN
    
    Number("42px") //NaN
    
    1 + undefined  //NaN
    1 + null  //1
    
    
  • toBoolean rules :

    • Five of the following enumerated values are false:
      1. undefined
      2. null
      3. to false
      4. + 0, -0, NaN3
      5. The ""
  • In addition to the above recited values, other values are real, such as {}, [], function ( ) {} and other objects are true value.
    Note: toBoolean a conversion rule, while the Boolean inherent converted into a digital method;

Example:

Boolean(undefined) //false
Boolean(null) //false
Boolean(0) //false
Boolean("") //false
Boolean(false) //false

Boolean({}) //true
Boolean([]) //true
Boolean("0") //true
Boolean(1) //true

The following case Boolean implicit conversion occurs: Following toBoolean abstract operation rules previously mentioned.

  1. if ( )
  2. for (...; ...; ...), the second expression is determined
  3. while() ;do …while()
  4. ?: Ternary operator
  5. The number of logical operators && and || left
  • || 和&&

Perhaps the "logical operators" call to "selector operator" is more accurate!
They return value is a two operands; i.e. a || b, a return or B; and this other languages (C / PHP) will return different Boolean value.

Example:

		var a = 43;
		var b = "ac";
		var c = null;
		
		a || b //43
		a && b //"ac"
		
		c || b // "ac"
		c && b //null

Its function is similar ternary operator. Example: a || b ===> a a :? B
used in this method to set the default values:

	function test (a,b){
		a = a ||  1
		b = b || 2
		console.log(a , b)
	}
	test() // 1,2
	test(4,5) //4,5

a && b: a is the guardian of the operator b, a return b is true; shorting rules.

Display Type conversion:

Converting between strings and numbers:

  • Converted into a digital date only added in front +Number: Example var timestamp = + new Date () ;
  • Differs parseInt of: Number ( "42px") == NaN and parseInt ( "42px") == 42

Example:

	Number(true) ;  //1
	Number([2])   //2
	+true ;  //1
	+ "123" ;  //123
	String(43)   // "43"
	 123.toString()  // "123"
	 String([1,2])  //  "1,2" ;
	 String({a:2.34}) // "[object Object]"
	+Date.now() ;  //1583563234264
	+new Date();   //1583563234264

	parseInt("42px") // 42  从第一位开始取,遇到非数字停下
	parseFloat("43.8px") //43.8  从第一位开始取,遇到非数字和 . 时停下

Cast to display a Boolean value:

  • !!: inversion in negation symbol

例: !!null === false , !!“a” === true

  • Boolean()

Boolean(null)===false

Example:

		!! ""  //false
		!!null //false
		!!undefined //false
		!! 123 //true
		!!{} //true

Implicit conversion

  • Converting between strings and numbers:

    Example:

    	123 +  ""  // "123"
    	1+"3"  // "13"
    	1+2 //3
    	
    	[1,2]+[3,4] //"1,23,4"  //先调用valueOf 不返回基本类型值,在调用 toString ,若还是无返回基本类型值,产生TypeError错误。
    	"3.14" - 0  //3.14
    
    
    
    
    
    
    
    
Published 18 original articles · won praise 10 · views 610

Guess you like

Origin blog.csdn.net/llq886/article/details/104713231