Fundamentals 2 Flashcards
How do you do a String Conversion?
String(value) function to convert a value to a string: let value = true; alert(typeof value); // boolean
value = String(value); // now value is a string “true”
alert(typeof value); // string
How do you do Numeric Conversions?
Numeric conversion happens in mathematical functions and expressions automatically:
alert( “6” / “2” ); // 3, strings are converted to numbers
We can use the Number(value) function to explicitly convert a value to a number: let str = "123"; alert(typeof str); // string
let num = Number(str); // becomes a number 123
alert(typeof num); // number
What’s an example of when you would need to use explicit Numeric Conversion?
When we read a value from a string-based source like a text form but expect a number to be entered.
If the string is not a valid number, the result of such a conversion is NaN. For instance:
let age = Number("an arbitrary string instead of a number"); alert(age); // NaN, conversion failed
What are four Numeric Conversion rules?
Value: Becomes… undefined: NaN null: 0 true and false: 1 and 0 string: Whitespaces from the start and end are removed. If the remaining string is empty, the result is 0. Otherwise, the number is “read” from the string. An error gives NaN.
What are examples of Boolean Conversions?
alert( Boolean(1) ); // true
alert( Boolean(0) ); // false
alert( Boolean(“hello”) ); // true
alert( Boolean(“”) ); // false
Note:
alert( Boolean(“0”) ); // true, as it is a string that contains something
“0” and space-only strings like “ “ are true as a boolean.
What are the four straightforward maths operations?
Addition +
Subtraction -
Multiplication *
Division /
How do you use the Remainder operator?
The remainder operator %, despite its appearance, is not related to percents.
The result of a % b is the remainder of the integer division of a by b.
For instance:
alert( 5 % 2 ); // 1, a remainder of 5 divided by 2
alert( 8 % 3 ); // 2, a remainder of 8 divided by 3
How do you use the Exponentiation operator?
The exponentiation operator a ** b raises a to the power of b.
In school maths, we write that as ab.
For instance:
alert( 2 ** 2 ); // 2² = 4
alert( 2 ** 3 ); // 2³ = 8
alert( 2 ** 4 ); // 2⁴ = 16
Just like in maths, the exponentiation operator is defined for non-integer numbers as well.
For example, a square root is an exponentiation by ½:
What happens when the addition operator is applied only to strings?
it merges (concatenates) them: let s = "my" + "string"; alert(s); // mystring
Note that if any of the operands is a string, then the other one is converted to a string too!!!
In this context, the + is called the binary plus.
What is the unary plus?
It is applied to a single value. It doesn’t do anything to numbers. But if the operand is not a number, the unary plus converts it into a number.
// No effect on numbers let x = 1; alert( +x ); // 1
let y = -2;
alert( +y ); // -2
// Converts non-numbers alert( +true ); // 1 alert( +"" ); // 0 It actually does the same thing as Number(...), but is shorter.
What is Operator precedence?
If an expression has more than one operator, the execution order is defined by their precedence, or, in other words, the default priority order of operators.
From school, we all know that the multiplication in the expression 1 + 2 * 2 should be calculated before the addition. That’s exactly the precedence thing. The multiplication is said to have a higher precedence than the addition.
Aka. BODMAS
What are these Assignment operators the same as? Operator then Example = x = y \+= x += y -= x -= y *= x *= y /= x /= y %= x %= y
= x = y x = y \+= x += y x = x + y -= x -= y x = x - y *= x *= y x = x * y /= x /= y x = x / y %= x %= y x = x % y
What is Increment?
Increasing a number by one is among the most common numerical operations.
So, there are special operators for it:
Increment ++ increases a variable by 1:
let counter = 2; counter++; // works the same as counter = counter + 1, but is shorter alert( counter ); // 3
What is Decrement?
Decreasing a number by one is among the most common numerical operations.
So, there are special operators for it:
Decrement – decreases a variable by 1:
let counter = 2; counter--; // works the same as counter = counter - 1, but is shorter alert( counter ); // 1
How does the comma operator work?
The comma operator , is one of the rarest and most unusual operators. Sometimes, it’s used to write shorter code, so we need to know it in order to understand what’s going on.
The comma operator allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated but only the result of the last one is returned.
Here, the first expression 1 + 2 is evaluated and its result is thrown away. Then, 3 + 4 is evaluated and returned as the result. let a = (1 + 2, 3 + 4);
alert( a ); // 7 (the result of 3 + 4)