Fundamentals 2 Flashcards

1
Q

How do you do a String Conversion?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you do Numeric Conversions?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s an example of when you would need to use explicit Numeric Conversion?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are four Numeric Conversion rules?

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are examples of Boolean Conversions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the four straightforward maths operations?

A

Addition +
Subtraction -
Multiplication *
Division /

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you use the Remainder operator?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you use the Exponentiation operator?

A

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 ½:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens when the addition operator is applied only to strings?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the unary plus?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is Operator precedence?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What are these Assignment operators the same as?
Operator then Example
=	x = y
\+=	x += y	
-=	x -= y	
*=	x *= y	
/=	x /= y	
%=	x %= y
A
=	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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Increment?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Decrement?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does the comma operator work?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why do we need an operator that throws away everything except the last expression (comma operator)?

A

Sometimes, people use it in more complex constructs to put several actions in one line.

For example:

// three operations in one line
for (a = 1, b = 3, c = a * b; a < 10; a++) {
 ...
}

Such tricks are used in many JavaScript frameworks. That’s why we’re mentioning them. But usually they don’t improve code readability so we should think well before using them.

17
Q

What are comparison operators syntax in js?

A
==	equal to
===	equal value and equal type
!=	not equal
!==	not equal value or not equal type
>	greater than
<	less than
>=	greater than or equal to	
<=	less than or equal to
18
Q

How do string comparisons work|?

A

Strings are compared letter-by-letter.

For example:

alert( ‘Z’ > ‘A’ ); // true
alert( ‘Glow’ > ‘Glee’ ); // true
alert( ‘Bee’ > ‘Be’ ); // true

19
Q

How does comparing different types work in javascript?

A

When comparing values of different types, JavaScript converts the values to numbers.

For example:

alert( ‘2’ > 1 ); // true, string ‘2’ becomes a number 2
alert( ‘01’ == 1 ); // true, string ‘01’ becomes a number 1
For boolean values, true becomes 1 and false becomes 0.

20
Q

A regular equality check == has a problem. It cannot differentiate 0 from false:

alert( 0 == false ); // true
This happens because operands of different types are converted to numbers by the equality operator ==.

What to do if we’d like to differentiate 0 from false?

A

A strict equality operator === checks the equality without type conversion.

In other words, if a and b are of different types, then a === b immediately returns false without an attempt to convert them.

Let’s try it:

alert( 0 === false ); // false, because the types are different

21
Q

What do comparison operators return?

A

Comparison operators return a boolean value.

22
Q

What does the if statement do?

A

The if(…) statement evaluates a condition in parentheses and, if the result is true, executes a block of code.

For example:

let year = prompt(‘In which year was ECMAScript-2015 specification published?’, ‘’);

if (year == 2015) alert( ‘You are right!’ );

23
Q

How do you execute more than one statement within the if statement?

A

If we want to execute more than one statement, we have to wrap our code block inside curly braces:

if (year == 2015) {
  alert( "That's correct!" );
  alert( "You're so smart!" );
}
We recommend wrapping your code block with curly braces {} every time you use an if statement, even if there is only one statement to execute. Doing so improves readability.
24
Q

Does the if statement convert the expression in its parantheses to boolean?

A

The if (…) statement evaluates the expression in its parentheses and converts the result to a boolean.

25
Q

What does the else clause do?

A

The if statement may contain an optional “else” block. It executes when the condition is falsy.

For example:

let year = prompt(‘In which year was the ECMAScript-2015 specification published?’, ‘’);

if (year == 2015) {
  alert( 'You guessed it right!' );
} else {
  alert( 'How can you be so wrong?' ); // any value except 2015
}
26
Q

What does else if do?

A

Sometimes, we’d like to test several variants of a condition. The else if clause lets us do that.

For example:

let year = prompt(‘In which year was the ECMAScript-2015 specification published?’, ‘’);

if (year < 2015) {
  alert( 'Too early...' );
} else if (year > 2015) {
  alert( 'Too late' );
} else {
  alert( 'Exactly!' );
}
27
Q

What is the conditional operator ‘?’ or ternary operator?

A

Sometimes, we need to assign a variable depending on a condition.

For instance:

let accessAllowed;
let age = prompt('How old are you?', '');
if (age > 18) {
  accessAllowed = true;
} else {
  accessAllowed = false;
}

alert(accessAllowed);

28
Q

What is the syntax for the ternary operator?

A

The syntax is:

let result = condition ? value1 : value2;
The condition is evaluated: if it’s truthy then value1 is returned, otherwise – value2.

For example:

let accessAllowed = (age > 18) ? true : false;
Technically, we can omit the parentheses around age > 18. The question mark operator has a low precedence, so it executes after the comparison >.

This example will do the same thing as the previous one:

// the comparison operator "age > 18" executes first anyway
// (no need to wrap it into parentheses)
let accessAllowed = age > 18 ? true : false;
But parentheses make the code more readable, so we recommend using them.

In the example above, you can avoid using the question mark operator because the comparison itself returns true/false:

// the same
let accessAllowed = age > 18;
29
Q

Example of multiple ternary operators vs if statement

A

let age = prompt(‘age?’, 18);

let message = (age < 3) ? ‘Hi, baby!’ :
(age < 18) ? ‘Hello!’ :
(age < 100) ? ‘Greetings!’ :
‘What an unusual age!’;

alert( message );

if (age < 3) {
  message = 'Hi, baby!';
} else if (age < 18) {
  message = 'Hello!';
} else if (age < 100) {
  message = 'Greetings!';
} else {
  message = 'What an unusual age!';
}
30
Q

Should you use the ternary operator over an if statement in the way an if statement would be used? When should you use one or the other?

A

It’s not recommended to use the question mark operator in this way.

The notation is shorter than the equivalent if statement, which appeals to some programmers. But it is less readable.

The purpose of the question mark operator ? is to return one value or another depending on its condition. Please use it for exactly that. Use if when you need to execute different branches of code.