JavaScript (iv): Operators & Data type conversions

Source: Internet
Author: User
Tags bitwise operators object object

+: addition of operator; connection string

Addition will automatically convert other types of values to strings, and then join operations!

1 varA=1+2;2Console.log (' First: ' +a);3 vara=1+2+ ' 3 ';//calculate the 1+2 first and then connect to the ' 3 ' string4Console.log (' Second: ' +a);5 varx=10;6 varA=x+ ";//Convert a value to a string7Console.log (' third: ' +a);8Note the following situation9 varnow=NewDate ();TenConsole.log (typeof(now+2));//string OneConsole.log (typeof(now-1));// Number

There are 9 operators in javascript:

+(addition: addition),-(addition: subtraction),*(multiplication: multiplication),/(Division: Division), %(remainder: remainder), ++(self-increment: Increment),--(self-subtract: decrement),+(numeric operator: Convert To Number;+x),-(negative numeric operator: negate;-x)

1Console.log (1+2);//32Console.log (1-2);//-13Console.log (1*2);//24Console.log (3/2);//1.55Console.log (' The symbol of the result of the remainder operation is determined by the symbol of the first operator ');6Console.log (3%2);//17Console.log ( -3%2);//-18 //the numeric operator (+) requires only one operand, and the addition requires two operands9 //the numeric operator (+) has the same effect as number ()TenConsole.log (' numeric operator '); OneConsole.log (+ ' 1 ');//1 AConsole.log (+false);//0 -Console.log (+[]);//0

Assignment operators & Compound operators: Note that symbols are linked together (for example, + = cannot be written as + =, that is, there are no spaces in the middle). )

1 varx=1,y=2;2X=y;//assignment operator, assigning y values to x3X+=y;//namely X=x+y;4X-=y;//x=x-y;5X*=y;//x=x*y;6x/=y;//x=x/y;7X%=y;//x=x%y;8x>>=y;//x=x>>y;9x<<=y;//x=x<<y;TenX >>>=y;//x=x>>>y; Onex &= y;//x=x&y; Ax |=y;//x=x|y; -x ^=y;//x=x^y;

8 comparison operators : = = (equal, equal), = = = (strictly equal, Strict equal),! = (unequal),! = = (not strictly equal),< (less than), <= (less than equals),> (greater than), >= (greater than or equal), = (equals)

After comparing the results, return the Boolean value!

1 console.log (2>1); // true 2 console.log (' cat ' > ' cat '); // True,ascii code c is bigger than C 3 console.log (' CatDog ' > ' cat '); // true 4 console.log (' abc ' > ' Bcde '); // false

When comparing objects:

1 //when the object is compared, the call ValueOf () method is usually called first, or if the object is returned, the call ToString () method2Console.log ('---');3 varRESULT=[2] > ' 11 ';//equivalent to [2].valueof (). toString () > ' One '4 Console.log (result);5 varresult1=[2]>[1];//equivalent to [2].valueof (). ToString () >[1].valueof (). ToString ()6 varresult2={x:1}>={y:2};//{x:1}.valueof (). ToString () >={y:2}.b=valueof (). ToString (); "[Object Object] ' >= ' [Object Object] '7Console.log (RESULT1,RESULT2);

the insufficiency of the = = operator : Therefore, use the = = operator as sparingly as possible (not even used) = = = Operator for related operations!

1 console.log (false = = ' false '); // false 2 console.log (null = = undefined); // true 3 Console.log (false = = "); // true 4 Console.log (falsenull); // false

bitwise operators : There are 7 direct operations on BITS:

or an operation (or):| ; Two bits are 0, the result is 0; otherwise 1

With operation (and):&, two bits are 1, the result is 1; otherwise 0

No operation (not):~; negation of bits

XOR:^; two bits different, 1; otherwise 0

Left shift:<<<<n: equal to 2 of the n-th square

Right Shift:>>>>n: equal to n times divided by 2

Right-shift operation with sign bit (zero filled R shift):>>>

Bit operations directly processing each bit, is very low-level operation, the operating speed is very fast , the disadvantage is not intuitive, many occasions can not be used, otherwise the code is difficult to understand and wrong!

bit operations only work on integers : in JavaScript, values are stored as 64-bit floating-point numbers, but in-place operations are performed with a 32-bit signed integer, and the return value is a 32-bit signed integer!

The conversion to the corresponding integer can be: (| ; leave the fractional part, leaving only the integer part)

Examples of no operations :

1 console.log ('---'); 2 Console.log (~ 3); // 4 can simply remember to add a value to its own inverse value to get-1 3 Console.log (~ 3); // two times no operation gets itself 4 Console.log (~ 3.1); // highest speed for rounding effect

Examples of XOR operations :

1 console.log (1^2); 2 // Exchange two data values without introducing a temporary variable 3 var a=99,b=100; 4 a^=b; 5 b^=A; 6 a^=b; 7 Console.log (A, b); // 100,99 8 Console.log (10.5^0); // Rounding effect

void operator : Executes an expression that does not return any value, or it can be said to return undefined

One of the main functions of the void operator is to insert code into the hyperlink, return undefined, and prevent the page from jumping .

1 <Body>2     <!--A jump will occur at this time -3     <ahref= "www.test.com"onclick= "f ();">Test</a>4     <!--No jump occurs at this time -5     <ahref= "www.test.com"onclick= "f (); return false;">Test1</a>6     <!--No jump occurs at this time -7     <ahref= "javascript:void (f ())">Test2</a>8     <Script>9         functionf () {Ten             return false; One         } A     </Script> -     <!--The following can make the page not jump when submitting the form - -     <ahref= "Javascript:void (Document.form.submit ())">Test3</a> the </Body>

() can improve the priority of operations, in order to structure clearly, it is recommended that complex formulas are always added ()!

Note: Only expressions can be placed in ();

Data type conversions : forcing data type conversions; Automatic data type conversions

Cast: Number (), String (), Boolean () three constructors!

Number ():

Note the number () cast of the object: valueof first, then ToString

String ():

Note the forced conversion of a string () to an object: ToString First, then valueof

Boolean (): Undefined,null,0,nan, "or" "translates to false; the remainder is true!

Others, such as [],{}, are true.

Automatic conversion : The recommended place to always add number (), String (), Boolean (), for checking and troubleshooting!

Reference: Nanyi JavaScript Standard Reference Tutorial

JavaScript (iv): Operators & Data type conversions

Related Article

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.