Top 10 Most Used Javascript Strings and Numbers methods:

How to create a string in javascript??

let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(txt) // output:"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const stringAsObject= new String("A String as an object");
console.log(typeof(stringAsObject)); //object
var str= "I am a string";
var strLength= txt.length;
console.log(strLength) // output: 13//
// length count counts extra space//
const  str = "Please locate where 'beautiful' word lies!";
const pos = str.indexOf("beautiful"); //Output: 7 //
const str = "Car, Boat, Bike";
var res= str.slice(7, 11); //Output:Boat //
const str = "Car, Boat, Bike";
var res= str.substring(7, 11); //Output:Boat //
const str = "Car, Boat, Bike";
var res= str.substring(7, 10); //Output:Boa //

Let’s talk about Numbers.

const  x = 3.14;    // A number with decimals
const y = 3; // A number without decimals
const x = 123e5;    // 12300000
const y = 123e-5; // 0.00123

toString():

const  num= 123;
x.toString(); // returns 123 from variable num
(123).toString(); // returns 123 from literal 123
(100 + 23).toString(); // returns 123 from expression 100 + 23

toFixed():

const x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000

Number():

Number(true);          // returns 1
Number(false); // returns 0
Number("10"); // returns 10
Number(" 10"); // returns 10
Number("10 "); // returns 10
Number(" 10 "); // returns 10
Number("10.33"); // returns 10.33
Number("10,33"); // returns NaN
Number("10 33"); // returns NaN
Number("John"); // returns NaN

parseInt():

parseInt("10");         // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN

parseFloat():

parseFloat("10");        // returns 10
parseFloat("10.33"); // returns 10.33
parseFloat("10 20 30"); // returns 10
parseFloat("10 years"); // returns 10
parseFloat("years 10"); // returns NaN

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store