Skip to main content

JS - DATA TYPE CONVERSION

DATA TYPE CONVERSIONS

NUMBER  to STRING

Var a = 10;

Var b = String(a)

STRING to NUMBER

Var a = ‘10’;

Var b = Number(a) ; parseInt (a) ; parseFloat(a) ;   +(a)

For Non-Numeric string the output will be NaN

STRING to ARRAY

Var a =’ok’

Var b = a.split(‘’) ; a.split(‘  ‘) space ;  a.split(‘,’) comma

ARRAY to STRING

Var a = [1,2,3,4,5]

Var b = a.join(‘’) ; a.join(‘  ‘) space ;  a.join(‘,’) comma

ARRAY to OBJECT

Var arr = [1,2,3,4,5]

Var b = Object.assign({},arr)

OBJECT to ARRAY

Var  obj ={“name” : “susi” , “city” :”Trichy”, }

Var arr = Object.keys(obj)  ; Object.values(obj) ; Object.entries(obj)


// Number to String
var num = 25;
var str = String(num);
console.log(str)

// String to Number
var str = '2.5';
var num = Number(str);
var flo = parseFloat(str);
var int = parseInt(str);
var plus = +str;
console.log(num)
console.log(flo)
console.log(int)
console.log(plus)

//String to Array
var str ="javascript"
var arr = str.split('')
console.log(arr)

//Array to String
var arr = [1,2,3,"js"]
var str = arr.join('')
console.log(str)

//Array to Object
var arr = [1,2,3,"js"]
var obj = Object.assign({},arr)
console.log(obj)

//Object to Array
var obj = {"name":"hari","city":"trichy"}
var arr1 = Object.keys(obj)
var arr2 = Object.values(obj)
var arr3 = Object.entries(obj)
console.log(arr1)
console.log(arr2)
console.log(arr3)



Comments

Popular posts from this blog

  You are provided with the radius of a circle "A". Find the length of its circumference. Note: In case the output is coming in decimal, roundoff to 2nd decimal place. In case the input is a negative number, print "Error". function   coc () { var   r = document . getElementById ( "num1" ). value ; var   pi = 3.1415 ; var   Circumference =( 2 * pi * r ); if  ( r >= 0 ) console . log ( Circumference ); else console . log ( "Error" )} 
The area of an equilateral triangle is ¼(√3a 2 ) where "a" represents a side of the triangle. You are provided with the side "a". Find the area of the equilateral triangle. function   sqrt () { var   a = document . getElementById ( "a" ). value ; b =  Math . sqrt ( 3 ); c =( 1 / 4 )* b * a * a ; console . log (  "Area =" + c )}

HOISTING

  HOISTING Variables” and “ function” declarations are moved to the top of their scope before code execution. Variables are hosted and not their values Only the normal function will be hoisted not the anonymous,IIFE and Arrow Function will be hoisted Properties are not hoisted PROPERTY  Any variable declared globally  become property of window variable declared without keyword variable declared with keyword var except function scope var console . log ( a )   // Error a not defined console . log ( a );  // undefined --> var a is hoisted not value var   a  =  20 ; console . log ( a , b )  // Error --->property b can't hoist var   a  =  20 ; b  =  40 ; var   a  = add ( 2 , 5 )  // Error console . log ( a ) var   a  = add ( 2 , 5 )  // 7 function add hoist console . ...