Sign in to Track Your Progress
Module
Sign in to Track Your Progress
By Mukul Latiyan
Updated on : 14 Oct 2022
17 mins read
Published on : 14 Oct 2022
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Implicitly Converting Type To String</h2>
<script>
let res;
res = '3' + 5;
console.log(res);
res = '3' + false;
console.log(res);
res = '3' + undefined;
console.log(res);
res = '3' + null;
console.log(res);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Implicitly Converting Type To A Number</h2>
<script>
let res;
res = '5' - 3;
console.log(res);
res = '5' - '2';
console.log(res);
res = '5' / '2';
console.log(res);
res = '5' * '3';
console.log(res);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Implicitly converting from a Boolean to a Number</h2>
<script>
let res;
res = 5 - false;
console.log(res);
res = 5 - true;
console.log(res);
res = 5 / true;
console.log(res);
res = 5 * false;
console.log(res);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Implicitly converting null to Number</h2>
<script>
let res;
res = null + 5;
console.log(res);
res = null - 5;
console.log(res);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>String to a Number</h2>
<script>
let str = '123';
// Using Number function
console.log(Number(str));
// Using parseInt function
console.log(parseInt(str));
// Using + operator
console.log(+str);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Boolean to a Number</h2>
<script>
let bool = true;
// Using Number function
console.log(Number(bool));
// Using multiplication
console.log(bool * 1);
// Using + operator
console.log(+bool);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Date to a Number</h2>
<script>
let dt = new Date();
// Using getTime method
console.log(dt.getTime());
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Number to String</h2>
<script>
let num = 25;
// Using toString method
console.log(num.toString());
// Using String conversion
console.log(String(num));
// Using + symbol
console.log(''+ num);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>Boolean to String</h2>
<script>
let val = true;
// Using toString method
console.log(val.toString());
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Type Conversion in JavaScript</title>
</head>
<body>
<h1>Example</h1>
<h2>to boolean</h2>
<script>
let str = 0;
// Using Boolean method
console.log(Boolean(str));
str = "";
// Using Boolean method
console.log(Boolean(str));
str = NaN;
// Using Boolean method
console.log(Boolean(str));
str = null;
// Using Boolean method
console.log(Boolean(str));
</script>
</body>
</html>