1 / 43

Operators and Expressions

Operators and Expressions. Performing Simple Calculations with JavaScript. Doncho Minkov. Telerik Software Academy. http://academy.telerik.com. Technical Trainer. http://minkov.it. Table of Contents. Operators in JavaScript Operator Precedence Arithmetic Operators Logical Operators

Download Presentation

Operators and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Operators and Expressions Performing Simple Calculations with JavaScript Doncho Minkov Telerik Software Academy http://academy.telerik.com Technical Trainer http://minkov.it

  2. Table of Contents • Operators in JavaScript • Operator Precedence • Arithmetic Operators • Logical Operators • Bitwise Operators • Comparison Operators • Assignment Operators • Other Operators • Expressions

  3. Operators in JavaScript Arithmetic, Logical, Comparison, Assignment, Etc.

  4. What is an Operator? • Operator is an operation performed over data at runtime • Takes one or more arguments (operands) • Produces a new value • Operators have precedence • Precedence defines which will be evaluated first • Expressions are sequences of operators and operands that are evaluated to a single value

  5. Operators in JavaScript • Operators in JavaScript : • Unary – take one operand • Binary – take two operands • Ternary (?:) – takes three operands • Except for the assignment operators, all binary operators are left-associative • The assignment operators and the conditional operator (?:) are right-associative

  6. Categories of Operators in JS

  7. Operators Precedence

  8. Operators Precedence

  9. Operators Precedence (2) • Parenthesis operator always has highest precedence • Note: prefer using parentheses, even when it seems stupid to do so

  10. Arithmetic Operators

  11. Arithmetic Operators • Arithmetic operators +,-, *are the same as in math • Division operator /if used on integers returns integer (without rounding) or exception • Division operator / if used on real numbers returns real number or Infinity or NaN • Remainder operator% returns the remainder from division of integers • The special addition operator ++ increments a variable

  12. Arithmetic Operators – Example var squarePerimeter = 17; var squareSide = squarePerimeter / 4.0; var squareArea = squareSide * squareSide; console.log(squareSide); // 4.25 console.log(squareArea); // 18.0625 var a = 5; var b = 4; console.log( a + b ); // 9 console.log( a + b++ ); // 9 console.log( a + b ); // 10 console.log( a + (++b) ); // 11 console.log( a + b ); // 11 console.log(12 / 3); // 4 console.log(11 / 3); // 3.6666666666666665

  13. Arithmetic Operators –Example (2) console.log(11.0 / 3); // 3.666666667 console.log(11 / 3.0); // 3.666666667 console.log(11 % 3); // 2 console.log(11 % -3); // 2 console.log(-11 % 3); // -2 console.log(1.5 / 0.0); // Infinity console.log(-1.5 / 0.0); // -Infinity console.log(0.0 / 0.0); // NaN var x = 0; console.log(5 / x);

  14. Live Demo Arithmetic Operators

  15. Logical Operators

  16. Logical Operators • Logical operators take boolean operands and return boolean result • Operator !turns true to false and falsetotrue • Behavior of the operators &&, ||and ^(1== true, 0== false) :

  17. Logical Operators – Example • Using the logical operators: var a = true; var b = false; console.log(a && b); // False console.log(a || b); // True console.log(a ^ b); // 1 console.log(!b); // True console.log(b || true); // True console.log(b && true); // False console.log(a || true); // True console.log(a && true); // True console.log(!a); // False console.log((5>7) ^ (a==b)); // 0

  18. Live Demo Logical Operators

  19. Bitwise Operators

  20. Bitwise Operators • Bitwise operator ~turns all 0 to 1 and all 1 to 0 • Like !for boolean expressions but bit by bit • The operators |,& and^ behave like ||,&& and^ for boolean expressions but bit by bit • The << and >> move the bits (left or right) • Behavior of the operators|,& and^:

  21. Bitwise Operators (2) • Bitwise operators are used on integer numbers • Bitwise operators are applied bit by bit • Examples: var a = 3; // 00000000 00000011 var b = 5; // 00000000 00000101 console.log( a | b); // 00000000 00000111 console.log( a & b); // 00000000 00000001 console.log( a ^ b); // 00000000 00000110 console.log(~a & b); // 00000000 00000100 console.log( a << 1); // 00000000 00000110 console.log( a >> 1); // 00000000 00000001

  22. Bitwise Operators – Tips & Tricks • How to get the bit at position p in a number n? • How to set the bit at position p to 0? var p = 5; var n = 35; // 00000000 00100011 var mask = 1 << p; // 00000000 00100000 var nAndMask = n & mask; // 00000000 00100000 var bit = nAndMask >> p; // 00000000 00000001 console.log(bit); // 1 var p = 5; var n = 35; // 00000000 00100011 var mask = ~(1 << p); // 11111111 11011111 var result = n & mask; // 00000000 00000011 console.log(result); // 3

  23. Bitwise Operators – Tips & Tricks (2) • How to set the bit at position p to 1? var p = 4; var n = 35; // 00000000 00100011 var mask = 1 << p; // 00000000 00010000 var result = n | mask; // 00000000 00110011 console.log(result); // 3

  24. Live Demo Bitwise Operators

  25. Comparison and Assignment Operators

  26. Comparison Operators • Comparison operators are used to compare variables • ==,<,>,>=,<=,!= • Comparison operators example: var a = 5; var b = 4; console.log(a >= b); // True console.log(a != b); // True console.log(a == b); // False console.log(a == a); // True console.log(a != ++b); // False console.log(a > b); // False

  27. Assignment Operators • Assignment operators are used to assign a value to a variable • =,+=,-=,|=,... • Assignment operators example: var x = 6; var y = 4; console.log(y *= 2); // 8 var z = y = 3; // y=3 and z=3 console.log(z); // 3 console.log(x |= 1); // 7 console.log(x += 3); // 10 console.log(x /= 2); // 5

  28. Comparison and Assignment Operators Live Demo

  29. Other Operators

  30. Other Operators • String concatenation operator +is used to concatenate strings • If the second operand is not a string, it is converted to string automatically var first = "First"; var second = "Second"; console.log(first + second); // FirstSecond var output = "The number is : "; var number = 5; console.log(output + number); // The number is : 5

  31. Other Operators (2) • Member access operator . is used to access object members • Square brackets []are used with arrays indexers and attributes • Parentheses()are used to override the default operator precedence

  32. Other Operators (3) • Conditional operator ?: has the form (if b is true then the result is x else the result is y) • The typeof(…)operator returns the type of its operand • The new operator is used to create new objects • The Number(…),String(…),Boolean(…), operator converts the type of the object b ? x : y

  33. Other Operators – Example • Using some other operators: var a = 6, b = 4; console.log(a > b ? "a>b" : "b>=a"); var c = b = 3; console.log(c); console.log((a+b)/2); console.log(typeof(2)); console.log((a+b)/2); // 4.5 console.log(new Date()); // the current date console.log(Boolean((a+b)/2)); // true console.log(Number("a33")); // NaN console.log(Number("33.00")); // 33

  34. Live Demo Other Operators

  35. Expressions

  36. Expressions • Expressions are sequences of operators, literals and variables that are evaluated to some value • Examples: var r = (150-20) / 2 + 5; // r=70 // Expression for calculation of circle area var surface = Math.PI * r * r; // Expression for calculation of circle perimeter var perimeter = 2 * Math.PI * r;

  37. Expressions (2) • Expressions has: • Type (integer, real, boolean, ...) • Value • Examples: Expressions of type integer. Calculated at runtime. Expression of type integer. Calculated at compile time. var a = 2 + 3; // a = 5 var b = (a+3) * (a-4) + (2*a + 7) / 4; // b = 12 var greater = (a > b) || ((a == 0) && (b == 0)); Expression of type boolean. Calculated at runtime.

  38. Expressions Live Demo

  39. Resources • Boolean algebra (logic) • http://en.wikipedia.org/wiki/Boolean_algebra_%28logic%29 • Bitwise mask • http://en.wikipedia.org/wiki/Mask_%28computing%29 • Bitwise operation • http://en.wikipedia.org/wiki/Bitwise_operation • Bit Twiddling Hacks • graphics.stanford.edu/~seander/bithacks.html

  40. Operators and Expressions http://academy.telerik.com

  41. Exercises • Write an expression that checks if given integer is odd or even. • Write a boolean expression that checks for given integer if it can be divided (without remainder) by 7 and 5 in the same time. • Write an expression that calculates rectangle’s area by given width and height. • Write an expression that checks for given integer if its third digit (right-to-left) is 7. E. g. 1732 true. • Write a boolean expression for finding if the bit 3(counting from 0) of a given integer is 1 or 0. • Write an expression that checks if given print (x, y) is within a circle K(O, 5).

  42. Exercises (2) • Write an expression that checks if given positive integer number n (n≤ 100) is prime. E.g. 37 is prime. • Write an expression that calculates trapezoid's area by given sides a and band height h. • Write an expression that checks for given point (x, y) if it is within the circle K( (1,1), 3) and out of the rectangle R(top=1, left=-1, width=6, height=2). • Write a boolean expression that returns if the bit at position p (counting from 0) in a given integer number v has value of 1. Example: v=5; p=1 false.

  43. Exercises (3) • Write an expression that extracts from a given integer i the value of a given bit number b. Example: i=5; b=2  value=1.

More Related