Javascript for Test Automation (E4)
Operations
Adding, Subtracting, and Multiplying numbers
//Use the + operator to perform addition:
> 38 + 4
= 42//Subtraction uses the '-' operator:
> 44 - 2
= 42//Multiplication uses the * operator:
> 7 * 6
= 42/*You can divide two numbers—integers or decimals—with the / operator.*/> 16 / 4
= 4> 16 / 2.5
= 6.4/* When you divide two integers that don't result in a whole number, you get a decimal number in the result:*/
> 16 / 5
= 3.2> 16 / 7
= 2.2857142857142856/* JavaScript also has a remainder operator, %. It returns the remainder of a division operation. Let's try it:*/> 16 % 4
= 0> 16 % 5
= 1
NaN
Some operations, such as 0 / 0
, return the value NaN
Which stands for "Not a Number." This exceptional value signals an illegal operation on numbers, such as 0 / 0
Or some other operation that doesn't make sense, such as 3 + undefined
.
> typeof NaN
= 'number'
Infinity and -Infinity
Other mathematical operations, such as 1 / 0, return Infinity, which denotes an infinitely large integer. What distinguishes it from NaN? It doesn’t matter if you have NaN or Infinity for most practical uses, but they are fundamentally distinct notions. Infinity is a number so enormous that it cannot be written down. You will never reach the value of Infinity if you start counting 1, 2, 3, 4, and so on, starting a new number every second for the next 37 trillion years. There will always be a higher value. NaN is the outcome of an attempted mathematical operation that is neither a fair number nor an infinite number.
Divide a positive number by 0 is a common operation that yields Infinity. For example, 1 divided by 0 (written 1 / 0) yields Infinity. Several other operations can return Infinity, but division by 0 is the most common.
> Infinity * Infinity
= Infinity> Infinity + Infinity
= Infinity> Infinity - Infinity
= NaN> Infinity / Infinity
= NaN> 1234567890 / Infinity
= 0> -1 / 0
= -Infinity> typeof Infinity
= 'number'> typeof -Infinity
= 'number'
Equality Comparison
/*You sometimes want to determine whether two values are identical. For that, you can use the === operator. It compares two operands for equality and returns true or false as appropriate.*/> 42 === 42
= true> 42 === 43
= false> 'foo' === 'foo' // It works with strings too
= true> 'FOO' === 'foo' // Case is different
= false
String Concatenation
/*String concatenation looks like arithmetic addition, but the result is entirely different. When you use the + operator to join two strings, you are concatenating their values.*/> 'foo' + 'bar'
= 'foobar'> '1' + '2'
= '12'> '1' + 2
= '12'> '5' - 3
= 2
Explicit Coercion
Assume you have two string values, ‘1’ and ‘2’, in your application that you want to add mathematically to obtain 3. You can’t do it directly since + concatenates when one of its operands is a string. We need to convert the strings ‘1’ and ‘2’ to the integers 1 and 2 in some way: we want to execute explicit type coercion. The distinction between explicit and implicit coercion is that explicit coercion allows you to select what to do, whereas implicit coercion allows the engine to pick.
//Strings to Numbers> Number('1')
= 1> Number('foo')
= NaN> parseInt('12')
= 12> parseInt('12xyz')
= 12> parseInt('3.1415')
= 3> parseFloat('12.5foo')
= 12.5//Numbers to Strings> String(20)
= '20'