[Original] javascript and PHP |, & operator similarities and differences in learning javascript, found logic and &, logic or | these two operators are very special in variable assignment, different from the familiar PHP!
1. condition statements
Condition statements, such as if judgment, are the same as and OR and have no objection:
(1) | when one of the conditions is true, the statement is executed.
(2) & if all conditions must be true, the statement is executed.
2. variable assignment
PHP:
$ Person = array (); $ param = $ arr ['name'] | 'Bom '; var_dump ($ param); // true, in PHP, the logic or condition determines the nature. the variable is a Boolean value var_dumo ($ param); // false. in PHP, the logic and condition determines the nature, A variable is a Boolean value.
The logic and or understanding of JS are as follows:
(1) | indicates that if the left value is true, the left value is returned. Otherwise, the right value is returned.
(2) & indicates that if true is returned on the left side, the right side is returned. Otherwise, the left side is returned.
What is the purpose? It can prevent the typeError exception from being thrown when the variable is assigned, or it can be used as the default value of the variable, for example:
Var person = {}; // empty object var undefinedRes = person. name; // undefined, attempts to retrieve a nonexistent member element and returns undefinedvar typeErrorRes = person. name. age; // typeError. if you try to retrieve an undefined value, the typeError exception var status = person. name | "Bom"; // Bom, with | use the default Bom. Person. name returns false, so take the logical or right value var name = person. name & "Bom"; // undefined, person. name returns false, so the logical and left values are used.