JavaScript Type Conversions

String to number conversion

parseInt() method are convert string values to a whole number (integer value).

parseInt(string, radix);

radix are optional by default that is based on according to decimal number system. You'll can use the radix value is 2 for binary, 8 for octal, 10 for decimal,16 for a hexadecimal value.

let data = "12345.78";
//Convert string to number
let result = parseInt(data);
//display type and values
console.log(typeof data ,data);
console.log(typeof result , result);
string 12345
number 12345

When in case given string text are with number and after that including some characters. Then this case this method are return first starting numbers.

let data = "12XXX34";
//Convert string to number
let result = parseInt(data);
//display type and values
console.log(typeof data ,data);
console.log(typeof result , result);
string 12XXX34
number 12

In case string text are not start with number this method are return NaN.

let data = "XXX1234";
//Convert string to number
let result = parseInt(data);
//display type and values
console.log(data); //XXX1234
console.log(result); //NaN
XXX1234
NaN

parseFloat() are similar to parseInt() method which is convert string value into floating point numbers.

let data = "78.9087XXX";
//Convert string to float value
let result = parseFloat(data);
//display type and values
console.log(data); //78.9087XXX
console.log(typeof result,result); //number 78.9087

Number() method are work both float and integer string text value. But string text contains not number such as character and special symbols, then this returns a NaN value.

console.log(Number("10.33"));
console.log(Number("10000"));
console.log(Number("10XX"));
console.log(Number("10.XX"));
10.33
10000
NaN
NaN

Convert boolean to int (number)

There is no method are available to convert boolean value to number but you'll can converted logically boolean value to number using ternary operator.

function convertBoolToInt(value){
    if("boolean" == typeof value){
    	return (value?1:0);
    }
    return NaN;
	
}
console.log(convertBoolToInt(true)); //1
console.log(convertBoolToInt(false)); //0
console.log(convertBoolToInt("11false")); //NaN
console.log(convertBoolToInt(11)); //NaN

typeof are used to check datatype of variables.

Convert JavaScript Array to JSON

let record = {};
record[0]="Mango";
record[1]="Oranges";
record[2]="Pineapple";
console.log(typeof record);
console.log(JSON.stringify(record));
object
{"0":"Mango","1":"Oranges","2":"Pineapple"}

Convert an object key and value pair into an array

//Define an object
var obj ={"1":10,"2":20,"3":30,"4":40,"5":50,"6":60,"7":70, "8" : 80,"9" : 90,"10" : 100}; 
//Convert an object key and value pair into an array
var data=Object.entries(obj);
console.log(obj);
console.log(data);
{ '1': 10,
  '2': 20,
  '3': 30,
  '4': 40,
  '5': 50,
  '6': 60,
  '7': 70,
  '8': 80,
  '9': 90,
  '10': 100 }
[ [ '1', 10 ],
  [ '2', 20 ],
  [ '3', 30 ],
  [ '4', 40 ],
  [ '5', 50 ],
  [ '6', 60 ],
  [ '7', 70 ],
  [ '8', 80 ],
  [ '9', 90 ],
  [ '10', 100 ] ]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved