|| And && all follow the "short-circuit" principle, as well as JS in Typeof__js

Source: Internet
Author: User
/**
*   in almost all languages | | and && Follow the principle of "short-circuit",
*   If the first expression in && is false, it will not handle the second expression, and | | Just the opposite.
*   JS also follow the above principles.
*   When | | , a sub-item found to true stops processing and returns the value of the sub-item, otherwise executed, and returns the value of the last sub-item.
*   when &&, a sub-item found to false stops processing and returns the value of the sub-item.
**/
//  var a = "" | | null | | 3 | | 4;//3
//  alert (a);  var b = 4 && 5 && null && "0";//null
//  alert (b);

a&& B: If a returns true after a, B is executed and the value of B is returned; If a returns false after a, the entire expression returns the value of a, and B is not executed;

A | | B: If a returns true after a, the entire expression returns the value of a, B is not executed, and if a returns false after a, executes B and returns the value of B; &&
Priority higher than | |;

Refer to the above content from: http://www.cnblogs.com/chenqingwei/archive/2010/05/14/1735174.html

The following reference from: http://blog.csdn.net/z18842589113/article/details/53315910 typeof

The typeof operator returns the type information as a string. There are six possible typeof return values: "Number," "String," "Boolean,"
"Object," "function," and "undefined." We can use TypeOf to get whether a variable exists, such as if (typeof
a!= "undefined") {}, and do not use if (a) because if a does not exist (not declared) will be an error, for special objects such as Array,null use TypeOf return object, which is the limitations of TypeOf.

parentheses in the typeof syntax are optional.

if (document.mylist.length!= "undefined") {} This usage is incorrect. The correct is if (
typeof (Document.mylist.length)!= "undefined") {} or if (
!isnan (Document.mylist.length)) {} typeof operand is undefined and returns "undefined".
Operands are numeric typeof (x) = "Number" string typeof (X) = "string" Boolean typeof (X) =
"Boolean" object, array and null typeof (X) = "Object" function typeof (X) = "function"
The typeof operator returns a String that represents the data type of an expression.
Possible strings are: "Number", "string", "Boolean", "Object", "function", and "undefined".

For special objects such as array,null to use TypeOf to return object, which is the limitations of TypeOf.
If we want to get an object to be an array, or to determine whether a variable is an instance of an object, choose to use instanceof. Instanceof is used to determine whether a variable is an instance of an object, such as the Var
a=new Array (); alert (a instanceof array); Returns true while alert (a instanceof
Object Also returns true, because the array is a subclass of object. Again, the function test () {};var a=new
Test (), alert (a instanceof test) returns True. Friendship Tip A Instanceof object
gets true not because the array is a child of object, but because the prototype property of the
array is constructed at the parent of Object,array is a function

Examples//Numbers typeof = = ' number ';
typeof 3.14 = = = ' number ';
typeof (+) = = ' number ';
typeof MATH.LN2 = = = ' number ';
typeof Infinity = = = ' number '; typeof NaN = = ' number '; Despite being "Not-a-number" typeof number (1) = = = ' number ';


But never use this form!
Strings typeof "" = = = ' String ';
typeof "Bla" = = = ' String '; typeof (typeof 1) = = ' String '; typeof always returns a string typeof string ("abc") = = ' String ';


But never use this form!
Booleans typeof true = = ' Boolean ';
typeof false = = = ' Boolean '; typeof Boolean (true) = = ' Boolean ';


But never use this form! Symbols typeof symbol () = = ' symbol ' typeof symbol (' foo ') = = ' symbol ' typeof symbol.iterator = = ' symbol '//Undefin
ed typeof undefined = = ' undefined ';
typeof declaredbutundefinedvariable = = ' undefined '; 


typeof undeclaredvariable = = ' undefined ';

Objects typeof {A:1} = = = = ' object '; Use Array.isarray or Object.prototype.toString.call//to differentiate regular objects from arrays Typeof [1, 2, 4] = = ' object ';


typeof new Date () = = ' object '; The following is confusing.
Don ' t use! 
typeof New Boolean (true) = = = ' object '; 
typeof new Number (1) = = = ' object ';


typeof New String ("abc") = = ' object ';
Functions typeof function () {} = = = ' function ';
typeof class C {} = = = ' function ';
typeof Math.sin = = ' function ';
NULL//This stands since the beginning of JavaScript typeof null = = ' object '; In the implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects is 0. Null is represented as the null pointer (0x00 in most platforms). Consequently, NULL had 0 as type tag, hence the bogus typeof return value.

(reference) A fix is proposed for ECMAScript (via a opt-in), but was rejected.

It would have resulted intypeof null = = ' null '.

Regular Expressions callable Regular expressions were a non-standard addition in some. typeof/s/= = ' function '; Chrome 1-12 Non-conform to ECMAScript 5.1 typeof/s/= = ' object '; Firefox 5+ conform to ECMAScript 5.1 Exceptions all current browsers expose a non-standard host object document.all WI

Th type Undefined.
typeof document.all = = ' undefined '; Although the specification allows custom type tags for non-standard exotic objects, it requires those type tags to be diff Erent from the predefined ones. The case of document.all has type tag ' undefined ' must be classified as a exceptional violation of the rules.

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.