Arithmetic and logical binary operators used in javascript


The truth table for the AND operation is:. Bitwise ANDing any number x with 0 yields 0. Bitwise ANDing any number x with -1 yields x. Performs the OR operation on each pair of bits. The truth table for the OR operation is:. Bitwise ORing any number x with 0 yields x. Bitwise ORing any number x with -1 yields Performs the XOR operation on each pair of bits. The truth table for the XOR operation is:. Bitwise XORing any number x with 0 yields x.

Performs the NOT operator on each bit. NOT a yields the inverted value a. The truth table for the NOT operation is:. The bitwise shift operators take two operands: The direction of the shift operation is controlled by the operator used. Shift operators convert their operands to bit integers in big-endian order and return a result of the same type as the left operand.

The right operand should be less than 32, but if not only the low five bits will be used. This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right. This operator shifts the first operand the specified number of bits to the right.

Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left. Since the new leftmost bit has the same value as the previous leftmost bit, the sign bit the leftmost bit does not change. Hence the name "sign-propagating". Zero bits are shifted in from the left. The sign bit becomes 0, so the result is always non-negative. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. However, this is not the case for negative numbers.

The bitwise logical operators are often used to create, manipulate, and read sequences of flags , which are like binary variables. Variables could be used instead of these sequences, but binary flags take much less memory by a factor of These flags are represented by a sequence of bits: When a flag is set , it has a value of 1. When a flag is cleared , it has a value of 0. Suppose a variable flags has the binary value Since bitwise operators are bit, is actually , but the preceding zeroes can be neglected since they contain no meaningful information.

Typically, a "primitive" bitmask for each flag is defined:. New bitmasks can be created by using the bitwise logical operators on these primitive bitmasks. Individual flag values can be extracted by ANDing them with a bitmask, where each bit with the value of one will "extract" the corresponding flag.

The void operator specifies an expression to be evaluated without returning a value. The parentheses surrounding the expression are optional, but it is good style to use them. You can use the void operator to specify an expression as a hypertext link.

The expression is evaluated but is not loaded in place of the current document. The following code creates a hypertext link that does nothing when the user clicks it. When the user clicks the link, void 0 evaluates to undefined , which has no effect in JavaScript. A relational operator compares its operands and returns a Boolean value based on whether the comparison is true.

The in operator returns true if the specified property is in the specified object. The instanceof operator returns true if the specified object is of the specified object type.

Use instanceof when you need to confirm the type of an object at runtime. For example, when catching exceptions, you can branch to different exception-handling code depending on the type of exception thrown. For example, the following code uses instanceof to determine whether theDay is a Date object.

Because theDay is a Date object, the statements in the if statement execute. The precedence of operators determines the order they are applied when evaluating an expression. You can override operator precedence by using parentheses. A more detailed version of this table, complete with links to additional details about each operator, may be found in JavaScript Reference.

Every syntactically valid expression resolves to some value but conceptually, there are two types of expressions: The expression itself evaluates to seven. JavaScript has the following expression categories:. Use the this keyword to refer to the current object. In general, this refers to the calling object in a method. Use this either with the dot or the bracket notation:. Suppose a function called validate validates an object's value property, given the object and the high and low values:.

You could call validate in each form element's onChange event handler, using this to pass it the form element, as in the following example:. The grouping operator controls the precedence of evaluation in expressions. For example, you can override multiplication and division first, then addition and subtraction to evaluate addition first.

There are two versions of comprehensions:. Comprehensions exist in many programming languages and allow you to quickly assemble a new array based on an existing one, for example. You can use the new operator to create an instance of a user-defined object type or of one of the built-in object types. Use new as follows:. The super keyword is used to call functions on an object's parent. The spread operator allows an expression to be expanded in places where multiple arguments for function calls or multiple elements for array literals are expected.

Today if you have an array and want to create a new array with the existing one being part of it, the array literal syntax is no longer sufficient and you have to fall back to imperative code, using a combination of push , splice , concat , etc. With spread syntax this becomes much more succinct:. Get the latest and greatest from MDN delivered straight to your inbox. Please check your inbox or your spam filter for an email from us. Document Tags and Contributors Tags: Beginner Expressions Guide JavaScript l10n: Contributors to this page: SphinxKnight , Feb 9, , 1: Learn the best of web development Get the latest and greatest from MDN delivered straight to your inbox.

I'm okay with Mozilla handling my info as explained in this Privacy Policy. Please check your inbox to confirm your subscription. Unsigned right shift assignment. Returns true if the operands are equal and of the same type. Returns true if the operands are of the same type but not equal, or are of different type. Adds one to its operand. Subtracts one from its operand. The return value is analogous to that for the increment operator.

If x is 3, then --x sets x to 2 and returns 2, whereas x-- returns 3 and, only then, sets x to 2. Returns a zero in each bit position for which the corresponding bits of both operands are zeros. Returns a zero in each bit position for which the corresponding bits are the same.

Shifts a in binary representation b bits to the left, shifting in zeros from the right. Shifts a in binary representation b bits to the right, discarding bits shifted off. Shifts a in binary representation b bits to the right, discarding bits shifted off, and shifting in zeros from the left.

This operator shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right. This operator shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.

Zero bits are shifted in from the left. For non-negative numbers, zero-fill right shift and sign-propagating right shift yield the same result. Returns expr1 if it can be converted to false ; otherwise, returns expr2.