String And Number methods in JavaScript.
Strings — Any character written inside quotes is considered as a string in JavaScript. If there is nothing inside the quotes then also it is considered as a string. We Can use single or double quotes. We can also use quotes inside a string but they should not match the quotes which are surrounding the string.
String methods
- length()
- charAt()
- indexOf()
- lastIndexOf()
- toUperCase()
- toLowercase()
- concat()
- trim()
- slice()
- replace()
- split()
- substring()
1. The length() Method
The length()
method returns the length of the string.
const string = "This is a string.";const strLength = string.length;console.log(`The length of the string is ${strLength}.`);
output : -
The length of the string is 17
2. The charAt() Method
The charAt()
method returns the character at specified index which is passed as a parameter in charAt().
const string = "This is a string.";console.log(string.charAt(5));
output :-
i
3. The indexOf() Method
The indexOf()
method returns the index of first occurrence of specified string which is passed as a parameter in indexOf().
const string = "This is a string.";console.log(string.indexOf("is"));
output :-
2
4. The lastIndexOf() Method
The lastIndexOf()
method returns the index of last occurrence of specified string which is passed as a parameter in indexOf().
const string = "This is a string.";console.log(string.lastIndexOf("is"));
output :-
5
5. The toUppercase() Method
The toUppercase()
method converts the string to uppercase
const str3 = "This is a String";const str3Up = str3.toUpperCase();console.log(str3Up);
output :-
THIS IS A STRING
6. The toLowercase() Method
The toLowercase()
method converts the string to uppercase
const str4 = "THIS IS A STRING";const str4Up = str4.toLowerCase();console.log(str4Up);
output :-
this is a string
7. The Concat() Method
The concat()
method merges two or more string into one
const str1 = "This is string 1.";const str2 = "This is String 2.";const concatenatedString = str1.concat(" ", str2);console.log(concatenatedString);
output :-
This is string 1. This is String 2.
8. The trim() Method
The trim()
method removes all the whitespaces from both starting and ending of the string.
const stringToTrim = " string to trim ";const trimmedString = stringToTrim.trim();console.log(trimmedString);
output : -
string to trim
9. The slice() Method
the slice()
method returns a part or slice of the given input string. The method takes 2 parameters: the starting index, and the ending index.
const slice = "2020-11-11t08:29:06Tz";const sliced = slice.slice(0,10)console.log(sliced);
output :-
2020-11-11
10. The replace() Method
The replace()
method replaces a specified value with another value in a string.
const strToReplace = "this is a string";console.log(strToReplace.replace("string", "replaced string"));
output :-
this is a replaced string
11. The split() Method
The split()
method converts a string into an array
const strToSplit = "this is a string";const splittedStr = strToSplit.split(' ');console.log(splittedStr);
output :-
["this", "is", "a", "string"]
12. The substring() Method
The substring()
method returns the part of the string between the start and end indexes which are passed as parameters in substring() method.
const string5 = "this is a string.";console.log(string5.substring(4,10));
output :-
is a
Numbers — In JavaScript there is only one type of number. It can be written with or without decimals.
Numbers methods
- toString()
- toExponential()
- toFixed()
- valueOf()
- Number()
- parseInt()
- parseFloat()
1. The toString() method
The toString()
method converts the number to string.
const num = 43516;const numToString = num.toString();console.log(numToString);
output :-
43516
2. The toExponential() method
The toExponential()
method converts a number to its exponential form.
Parameters :- it accepts a single parameter and it specifies the number of digits after the decimal.
const num2 = 43516;const numToExponential = num2.toExponential(2);console.log(numToExponential);
output :-
4.35e+4
3. The toFixed() method
The toFixed()
method formats a number using fixed-point notation.
parameter :- it accepts a single parameter and it specifies the number of digits after the decimal.
const num3 = 4.3516;const numToFixed = num3.toFixed(2);console.log(numToFixed);
output :-
4.35
4. The valueOf() method
The valueOf()
method in JavaScript is used to return the primitive value of a number.
const num4 = 4.3516;const numValue = num4.valueOf();console.log(numValue);
output :-
4.3516
5. The Number() method
The Number()
is used to convert the variables into numbers.
const str = "23561";const strToNum = Number(str);console.log(strToNum);
output :-
23561
6. The parseInt() method
The parseInt()
method converts the string to whole number.
const strr = "23.561";const str2ToNum = parseInt(strr);console.log(str2ToNum);
output :-
23
7. The parseFloat() method
The parseFloat()
method converts the string to floating point number.
const strr = "23.561";const str2ToNum = parseFloat(strr);console.log(str2ToNum);
output :-
23.561