Javascript Basic – How data type conversion is done

Home /

Table of Contents

Javascript Data Type Conversion

JavaScript is a loosely typed language, meaning that data type conversion is an important aspect of JavaScript programming. In JavaScript, data types can be automatically converted from one type to another as needed, which is known as implicit type coercion. However, sometimes it’s necessary to explicitly convert data types to ensure that the data is being used correctly. Explicit type conversion is done using built-in functions such as Number(), String(), Boolean(), and parseInt().

One common scenario where data type conversion is needed is when dealing with user input. When a user enters data into a form, the data is usually stored as a string. To perform calculations or other operations on this data, it needs to be converted to a number.

Sometimes we have to convert our data type from one type to another type.

Number from string & Boolean

To convert string and boolean into numbers there is a default method in Javascript called Number(). Where we pass the string and boolean variable to the method.

Example:

var string = '1000';
var bool = false;
 
console.log(Number(string));
console.log(Number(bool));

Output:

 1000
 0

String from Number & Boolean

To convert numbers and booleans into strings there is a default method in Javascript called String(). Where we pass the number and boolean variable to the method.

Example:

var number = 1000;
var bool = false;
 
console.log(String(number));
console.log(String(bool));

Output:

 1000
 false

Boolean from Number & String

To convert string and number into boolean there is a default method in Javascript called Boolean(). Where we pass the string and number variable to the method.

Example:

var number = 1;
var string = ' ';
 
console.log(Boolean(number));
console.log(Boolean(string));

Output:

 true
 false

Math Properties

Constant

The syntax for any Math property is: Math.property

OperationResult
Math.E returns Euler’s number
Math.PI returns PI
Math.SQRT2returns the square root of 2
Math.SQRT1_2returns the square root of 1/2
Math.LN2 returns the natural logarithm of 2
Math.LN10returns the natural logarithm of 10
Math.LOG2Ereturns base 2 logarithm of E
Math.LOG10Ereturns base 10 logarithm of E

Example:

console.log(Math.E); 
console.log(Math.PI);
console.log(Math.SQRT2);
console.log(Math.SQRT1_2);
console.log(Math.LN2);
console.log(Math.LN10);
console.log(Math.LOG2E);
console.log(Math.LOG10E);

Output:

2.718281828459045
3.141592653589793
1.4142135623730951
0.7071067811865476
0.6931471805599453
2.302585092994046
1.4426950408889634
0.4342944819032518

Math Methods

The syntax for Math any method is: Math.method(number)

OperationResult
Math.round(x)Returns x rounded to its nearest integer
Math.ceil(x)Returns x rounded up to its nearest integer
Math.floor(x)Returns x rounded down to its nearest integer
Math.trunc(x)Returns the integer part of x (new in ES6)

Example:

var x = 4.56
console.log(Math.round(x));
console.log(Math.ceil(x));
console.log(Math.floor(x));
console.log(Math.trunc(x));

Output:

5
5
4
4

Math Sign ( ES6 )

OperationResult
Math.sign( x )returns if x is negative, null or positive.
Math.sign(-4);return -1; that means the number is negative
Math.sign(0)return 0; that means the number is non-negative
Math.sign(4)return 1; that means the number is positive

Example:

console.log(Math.sign(-4));
console.log(Math.sign(0));
console.log(Math.sign(4));

Output:

-1
 0
 1

Math Object

Math.abs(num);
Math.floor(num);
Math.ceil(num);
Math.round(num);
Math.max(num 1, num 2, num 3);
Math.min(num 1, num 2, num 3);
Math.pow(base, power);
Math.sqrt(num);
Math.random();

Example:

var num = 3.456;
console.log(Math.abs(num));
console.log(Math.floor(num));
console.log(Math.ceil(num));
console.log(Math.round(num));
console.log(Math.max(400,500,300));
console.log(Math.min(400,500, 700));
console.log(Math.pow(2,4));
console.log(Math.sqrt(9));
console.log(Math.random());

Output:

3.456
3
4
3
500
400
16
3
0.5134705090767293

Date Object

A date is an object that can be used by calling a constructor function named “Date( )”. By default, JavaScript will use the browser’s time zone and display a date as a full-text string.

This date constructor function “Date ()” has some of its own methods.

Date objects are created with the new Date() constructor.

Creating Date Objects

For creating an object from the date constructor we have to use the “new” keyword.

new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)

Example:

var d = new Date() // Generate current date
console.log(d.toDateString());  // Generate current date in string
console.log(d.toTimeString());  // Generate current time with UTC in string
console.log(d.toLocaleString());// Generate current date and time

Output:

Sun Jun 12 2022
17:11:43 GMT+0600 (Bangladesh Standard Time)
6/12/2022, 5:11:43 PM

Note: Here is some useful handy dateObj method

Example:

console.log(d.getFullYear());   	// current year
console.log(d.getMonth());      	// current Month
console.log(d.getDate());       	// current Date
console.log(d.getHours());      	// current Hours
console.log(d.getMinutes());    	// current Minutes
console.log(d.getSeconds());      	// current Seconds
console.log(d.getMilliseconds()); 	// current Milliseconds

Output:

2022
5
12
17
11
43
417
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article