5. Type Conversion & Type Coercion Flashcards

1
Q

In How many types JavaScript can convert ?

A

2-3 Types

  • Number to a string to a Boolean
    But we cannot convert to undefined or NaN ( NaN - Not a Number ).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example of Type Conversion

String to a Number ( Reveal Answer )

A
const inputYear = '1991';
console.log(Number(inputYear) + 25);

In the above example we have converted string(1991) to a number

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

Examples of Type Coercion

Reval Answer

A

console.log(‘2’ + ‘2’)
Output will be 22

Type coercion will only work in Operator like ( -, *, /, **)

Like
console.log(‘5’ - ‘3’)
Output will be 2

console.log(‘2’ + ‘2’)
Output will be 22

Type coercion will only work in Operator like ( -, *, /, **)

Like
console.log(‘5’ - ‘3’)
Output will be 2

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

Coding Challenge :-

LECTURE: Type Conversion and Coercion
1. Predict the result of these 5 operations without executing them:
'9' - '5';
'19' - '13' + '17';
'19' - '13' + 17;
'123' < 57;
5 + 6 + '4' + 9 - 4 - 2;
2. Execute the operations to check if you were right
A

console. log(‘9’ - ‘5’); // -> 4
console. log(‘19’ - ‘13’ + ‘17’); // -> ‘617’
console. log(‘19’ - ‘13’ + 17); // -> 23
console. log(‘123’ < 57); // -> false
console. log(5 + 6 + ‘4’ + 9 - 4 - 2); // -> 1143

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