Skip to content

Latest commit

 

History

History
202 lines (189 loc) · 2.52 KB

7. Basic Type conversion.md

File metadata and controls

202 lines (189 loc) · 2.52 KB

7. Basic Type conversion

Type Conversions

const x = 8;
const y = 4;
const result = x + y;
console.log(result);

Output

12

const x = '8';
const y = 4;
const result = x + y;
console.log(result);

Output

84

const x = '8';
const y = 4;
const result = x + y;
console.log(result);
console.log(typeof(result));

Output

84
string

const x = 'hello';
const y = 4;
const result = x + y;
console.log(result);

Output

hello4

const result1 = '5' - '2';
const result2 = '5' - 2;
const result3 = '5' * 2;
const result4 = '5' / 2;

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);

Output

3
3
10
2.5

const result1 = '5' - '2';
const result2 = '5' - 2;
const result3 = '5' * 2;
const result4 = '5' / 2;

console.log(result1);
console.log(result2);
console.log(result3);
console.log(result4);

console.log(typeof(result1));
console.log(typeof(result2));
console.log(typeof(result3));
console.log(typeof(result4));

Output

3
3
10
2.5
number
number
number
number

const result = '4' - 'hello';
console.log(result);

Output

NaN

const result1 = '4' - true;
console.log(result1);

const result2 = '4' + true;
console.log(result2);

const result3 = 4 + false;
console.log(result3);

Output

3
4true
4

JavaScript Explicit Conversion

Explicit Numeric Conversion

const result = Number('324');
console.log(result);

console.log(typeof(result));

Output

324
number

const result = Number(true);
console.log(result);

console.log(typeof(result));

Output

1
number

Convert to String Explicitly

const result = String(257);
console.log(result);

console.log(typeof(result));

Output

257
string

const result1 = String(true);
console.log(result1);

console.log(typeof(result1));

const result2 = String(false);
console.log(result2);

console.log(typeof(result2));

Output

true
string
false
string

const result = (257).toString();
console.log(result);

console.log(typeof(result));

Output

257
string

Programiz Quiz

Q. What is the output of the following code?

console.log('5' - 3);
  1. 2
  2. 53
  3. Error
  4. NaN

Answer: 1