0% found this document useful (0 votes)
7 views4 pages

Conversion D5

Type conversion in JavaScript involves changing a value from one type to another, which can be done implicitly or explicitly. Implicit conversion occurs automatically, such as when numbers are added to strings, while explicit conversion requires functions like Number() or String(). The document details various conversion methods, including converting to strings, numbers, and handling boolean, null, and undefined values.

Uploaded by

aabidkhan70432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Conversion D5

Type conversion in JavaScript involves changing a value from one type to another, which can be done implicitly or explicitly. Implicit conversion occurs automatically, such as when numbers are added to strings, while explicit conversion requires functions like Number() or String(). The document details various conversion methods, including converting to strings, numbers, and handling boolean, null, and undefined values.

Uploaded by

aabidkhan70432
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Type Conversion

What is Type Conversion?


1) Type Conversion means changing a value from one type to another.
 Implicit Conversion: JavaScript does it automatically.
 Explicit Conversion: We do it ourselves.

a) Conversion to String
•Numbers or values added to strings are converted into strings

let result;
result = '3' + 2; // "32"
result = '3' + true; // "3true"
result = '3' + undefined; // "3undefined"
result = '3' + null; // "3null"

b) Conversion to Number
•Strings with -, *, or / are treated as numbers.
let result;
result = '4' - '2'; // 2
result = '4' * 2; // 8
result = '4' / 2; // 2
c) Boolean Conversion to Number
1) true → 1, false → 0.

result = 4 + true; // 5
result = 4 - false; // 4

d) null Conversion
1) null is treated as 0 in arithmetic operations.

result = 4 + null; // 4
result = 4 - null; // 4

e) undefined Conversion
1) Arithmetic with undefined results in NaN.

result = 4 + undefined; // NaN


result = true + undefined; // NaN
Explicit Conversion
We can convert values ourselves.

1. Convert to Number
•Use Number(), parseInt(), or parseFloat().

Number('123'); // 123
Number(true); // 1 (because true = 1)
Number(false); // 0 (because false = 0)
Number('abc'); // NaN (because 'abc' is not a number)
parseInt('20.5'); // 20 (removes decimal part)
parseFloat('20.5'); // 20.5 (keeps decimals)

Convert to String
We can turn numbers, booleans, or other types into strings.
String()
•Wraps the value as a string.
String(123); // "123"
String(true); // "true"
String(false); // "false"
String(null); // "null"
String(undefined); // "undefined"
1 Number to String
1️⃣

let num = 123;


let str = num.toString();
console.log(str); // "123" (string)
console.log(typeof str); // "string"

2️⃣Boolean to String

let bool = true;


let str = bool.toString();
console.log(str); // "true"
console.log(typeof str); // "string"

3️⃣Array to String

let arr = [1, 2, 3];


let str = arr.toString();
console.log(str); // "1,2,3"
console.log(typeof str); // "string"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy