1/21/2014

Operators | JavaScript Tutorial
Log in

JavaScript Tutorial
Home » Tutorial » JavaScript: from the Ground to Closures » First Steps » Operators and constructs
‹ User interaction: alert, prompt and confirm Comparison operators, if..else › Tutorial

JavaScript: from the Ground to Closures
JavaScript: from the Ground to Closures Javascript and related technologies First Steps
First Steps

Operators
Ilya Kantor

1. Arithmetic operators 2. String concatenation 3. Increment/decrement: + + ,4. Bitwise operators 1. Smart integer operations 5. Logical (boolean) operators 6. Assignment operators JavaScript has following basic operators…

Like

2

Setup your environment Hello, World! Variables and statements Browser Developer's Tools User interaction: alert, prompt and confirm Operators and constructs
Operators and constructs Operators Comparison operators, if..else Loops and switch

Arithmetic operators
Basic arithmetic operators are: + ,,,/ ,% . You can use them with variables and values like this:
Run!

Functions: declarations and expressions Mastering data types Four scents of "this" Type detection Function arguments Static variables and methods Scopes and Closures Decorators

1 v a ri=2 ; 2 i=( 2 + i )*3/i ; 3 a l e r t ( i ) Just to note. Integers division returns a float. In contrast to java, where 7 / 2 = 3(integer division), JavaScript will give you a normal 3 . 5 . Modulo operator %returns a remainder. i=5%2 ;/ /1 i=8%3 ;/ /2 i=6%3 ;/ /0

Document and Events Object Oriented Programming Timing Frames and windows Regular expressions in JavaScript Advanced and Extra stuff

String concatenation
Operator +contatenates strings: v a ra=" m y "+" s t r i n g "/ /m y s t r i n g If any of it’s arguments is a string, then another operand is coerced into a string too. Unlike many other languages, there is no difference which argument is a string: the left one or the right one. In either case a non-string operand is converted. For example:
Run!

Donate Donate to this project

1 a l e r t (' 1 '+2)/ /" 1 2 " 2 a l e r t (2+' 1 ')/ /" 2 1 "

Increment/decrement: + + ,These operators increment or decrement a variable by one. Unlike other arithmetic operators, they change a variable itself.
Run!

1 2 3 4 5 6 7

v a ri=3 i + + / /i n c r e m e n tt o4 a l e r t ( i ) i / /d e c r e m e n tt o3 a l e r t ( i )

You can also put + +and -before a variable (so-called *prefix form): i=3 ; + + i ;/ /n o w4 i ;/ /b a c kt o3 Besides changing the variable, these operators return a value.

http://javascript.info/tutorial/operators

1/4

9^0) / /1 2 2 a l e r t (1 3 . read the manual . 2 a l e r t ( + + a )/ /3 Bitwise operators Bitwise operators treat numbers as signed 32-bit integers. Especially. all of them cut off the decimal part. That is a side effect of inversing every bit of a number. double bitwise NOT doesn’t change the integer. bitwise operations are not very fast in JavaScript: So they shouldn’t be used low-level optimizations. Smart integer operations There are several tricks with bitwise operations to make the code shorter and faster. 5< <0)/ /1 3 http://javascript. but cuts off the decimal part: Run! 1 a l e r t (~ ~ 1 2 .1/21/2014 Which value is returned depends on the placement of + + ( ) . Bitwise NOT on an integer nis same as ( n + 1 ) .info/tutorial/operators 2/4 . the exotic zero-fill right shift. Most of them are rarely used. 3 4)/ /1 2 Other bitwise operators can be used to do the same: Run! 1 a l e r t (1 2 . If you are not familiar with them. For example. That’s because a single bitwise operation can give the same result as several ordinary numerical operations. For example: a l e r t (~ 1) / /2 a l e r t (~ 1)/ /0 Smart rounding Another use of binary operators is rounding. They are rarely used. so you can also skip the section. Smart -(n+1) The notable exception is bitwise NOT (~). If you are interested. Because they treat numbers as integers. read the general stuff from wikipedia Bitwise Operation article. Postfix form: i + + Returns a value and then increments: Run! Operators | JavaScript Tutorial 1 a=2 2 a l e r t ( a + + ) / /2 3 a l e r t ( a ) / /3 Prefix form: + + i Increments and then returns the value: Run! 1 a=2 . Following bitwise operators are supported: AND ( & ) OR ( | ) XOR ( ^ ) NOT ( ~ ) LEFT SHIFT ( << ) RIGHT SHIFT ( >> ) ZERO-FILL RIGHT SHIFT ( >>> ) Unlike C language.

A non-boolean value is converted into boolean for the purposes of evaluation. b The right shift a > > boperator is actually same as a / 2 . logical operators can be applied to anything . 2 / /t h ee v a l u a t i o ng o e st ot h er i g h to p e r a n d 3 a l e r t ( ' '| |n u l l ) / / n u l l . But binary operation always returns integer. JavaScript provides boolean evaluations.1/21/2014 Operators | JavaScript Tutorial Smart division by power of 2 Regular division by 2 may return a floating point number. they never evaluate more than needed.info/tutorial/operators 3/4 . which are 0.t h el a s te v a l u a t e dv a l u e / /L e f to p e r a n di sf a l s e . The operators are: Logical AND ( && ) Logical OR ( || ) Logical NOT ( ! ) Unlike bitwise operators.t h el a s te v a l u a t e dv a l u e What if both operands are falsy? Run! 1 / /L e f to p e r a n di sf a l s e . OR. / /O Rs t o p sa n dr e t u r n si ti m m e d i a t e l y a l e r t ( 1| |0 ) / /1 . See examples: Run! 1 a l e r t (5> >1)/ /i n t e g e rd i v i s i o nw i t h o u tar e m a i n d e r :5/2= 2 2 3 a l e r t (2 1> >2)/ /2 1/4=5 4 5 a l e r t (2 1> >3)/ /2 1/8=2 6 7 a l e r t (2 1> >4)/ /2 1/1 6=1 Logical (boolean) operators Like many other languages. 6 / /t h ee v a l u a t i o ng o e st ot h er i g h to p e r a n d 7 a l e r t ( 0| |1 ) / / 1 . Logical evaluations are short-circuit .t h el a s te v a l u a t e dv a l u e Logical OR is useful when you have multiple variables and need to get the first one which is true: Run! 1 2 3 4 5 6 7 v a rb=f a l s e v a rn=0 v a rs=" O l a l a " v a rr e s u l t=b| |n| |s a l e r t ( r e s u l t )/ /" O l a l a " Operator AND is similar: Run! 1 2 3 4 / /L e f to p e r a n di st r u e . NOT operate with boolean values. There is a short list of falsy values. empty string ” and few special values such as n u l land u n d e f i n e d . logical AND. / /A N Dn e e d st oc h e c kt h er i g h to p e r a n da l s o a l e r t ( 1& &0 ) / /0 .t h el a s te v a l u a t e dv a l u e http://javascript.t h el a s te v a l u a t e dv a l u e a l e r t ( 1& &5 ) / /5 . The result is the value which evaluated last. In other words. Run! 1 a l e r t (t r u e& &f a l s e)/ /f a l s e 2 3 a l e r t (f a l s e| |t r u e)/ /t r u e 4 5 a l e r t (! f a l s e)/ /t r u e In JavaScript. operator OR: Run! 1 2 3 4 5 / /L e f to p e r a n di st r u e . For example. All other values are t r u e .

> > = .6 ) 2 3 a l e r t ( a ) Brackets are put for correct priority.> > > = .i + +){ / /. prompt and confirm Comparison operators.” usually goes after assignment.| = . For example. i+ =5 ./ /1 4 Note.+ = ./ /7 i* =2 .else › The content of this site is available under the terms of CC BY-NC-SA. 2011.. . if. The following syntax is a valid comma operator example: Run! 1 a=( 5 .& = . http://javascript.t h el a s te v a l u a t e dv a l u e As of NOT operator " ! " . . Comma operator There is an “advanced” JavaScript operator. The list is: = . The result is always a boolean value. This site is not affiliated with Oracle corp.< < = . because there is no operator " | | = "(check the list above). it converts its operand into boolean and negates it. named the c o m m ao p e r a t o r .” and returns the value of the last one. © Ilya Kantor. Like this: f o r ( v a ri = 0 . A usage pattern may look like: i=2 . l e n g t h . boolean operators can’t be used like that. the code without brackets would evaluate as ( a = 5 ) . So.^ = .1/21/2014 Operators | JavaScript Tutorial 5 6 7 / /L e f to p e r a n di sf a l s e . Run! 1 a l e r t (! t r u e)/ /f a l s e 2 a l e r t (! 0) / /t r u e Double NOT is used to convert a value into boolean : Run! 1 a l e r t (! ! " s t r i n g ") / /t r u e 2 a l e r t (! ! n u l l)/ /f a l s e Assignment operators Most operators may be used as assignment. There is no real need in comma operator. The comma operator allows to list statements delimited by “. 8 / /A N Ds t o p si m m e d i a t e l ya n dr e t u r n si t 9 a l e r t ( n u l l& &5 ) / /n u l l . } In the example above. j = o b j ./ = .* = . ‹ User interaction: alert. because “. but it allows to put multiple statements in one line and made the code shorter. "JavaScript™ is a registered trademark of Oracle corp. 6 .j = o b j .i < j . l e n g t his merged with the loop start to evade extra line. a | | = bwill give error.= .info/tutorial/operators 4/4 .