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

  1. length()
  2. charAt()
  3. indexOf()
  4. lastIndexOf()
  5. toUperCase()
  6. toLowercase()
  7. concat()
  8. trim()
  9. slice()
  10. replace()
  11. split()
  12. substring()

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

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 :-


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 :-


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 :-


The toUppercase()method converts the string to uppercase

const str3 = "This is a String";const str3Up = str3.toUpperCase();console.log(str3Up);

output :-


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

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.

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

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 :-


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

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"]

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

  1. toString()
  2. toExponential()
  3. toFixed()
  4. valueOf()
  5. Number()
  6. parseInt()
  7. parseFloat()

The toString() method converts the number to string.

const num = 43516;const numToString = num.toString();console.log(numToString);

output :-


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 :-


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. 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 :-


The Number() is used to convert the variables into numbers.

const str = "23561";const strToNum = Number(str);console.log(strToNum);

output :-


The parseInt() method converts the string to whole number.

const strr = "23.561";const str2ToNum = parseInt(strr);console.log(str2ToNum);

output :-


The parseFloat() method converts the string to floating point number.

const strr = "23.561";const str2ToNum = parseFloat(strr);console.log(str2ToNum);

output :-


Computer Science Student, Front-end developer, love photography and paintings, now a blogger too i guess.