Top 10 Most Used Javascript Strings and Numbers methods:
Javascript is a hot programming language now. As a beginner, we should learn javascript in an effective way. A well-understood javascript programmer needs time for understanding all the methods and master them. But as a beginner, you will need these a lot
Strings: Strings are useful for holding data that can be represented in text form. It is widely used in our daily life.
How to create a string in javascript??
It’s so simple just declare a variable and assign value to it. You can use let, const, var before declaring a variable
let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(txt) // output:"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Another way to declare string with string() method: Javascript treats it’s like an object.
const stringAsObject= new String("A String as an object");
console.log(typeof(stringAsObject)); //object
Length: The length
property returns the length of a string:
var str= "I am a string";
var strLength= txt.length;
console.log(strLength) // output: 13//
// length count counts extra space//
indexOf(): It is used to find a specific string in a string
const str = "Please locate where 'beautiful' word lies!";
const pos = str.indexOf("beautiful"); //Output: 7 //
slice(start,end):
slice()
extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: the start position, and the end position (end not included).
const str = "Car, Boat, Bike";
var res= str.slice(7, 11); //Output:Boat //
substring(positive start,positive end) :
substring()
is similar to slice()
.
The difference is that substring()
cannot accept negative indexes.
const str = "Car, Boat, Bike";
var res= str.substring(7, 11); //Output:Boat //
substr(start,extract length):
substr()
is similar to slice()
.
The difference is that the second parameter specifies the length of the extracted part.
const str = "Car, Boat, Bike";
var res= str.substring(7, 10); //Output:Boa //
Let’s talk about Numbers.
JavaScript has only one type of number. Numbers can be written with or without decimals.
How to declare a numbers?
const x = 3.14; // A number with decimals
const y = 3; // A number without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation
const x = 123e5; // 12300000
const y = 123e-5; // 0.00123
toString():
The tostring() method returns a number as a string.
All number methods can be used on any type of numbers (literals, variables, or expressions) :
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():
toFixed() returns a string, with the number written with a specified number of decimals:
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()
can be used to convert JavaScript variables to numbers:
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()
parses a string and returns a whole number. Spaces are allowed. Only the first number is returned:
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()
parses a string and returns a number. Spaces are allowed. Only the first number is returned:
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