Top 11 JavaScript String functions that every developer must know


JavaScript is a programming language that is used to build server and client side applications and it has powerful functions to work with. JavaScript is a platform that is on number 1 for more than a decade in the industry.

JavaScript is a text-based programming language and can build interactive web pages, As it is directly browser familiar language it can dynamically work with HTML elements or does not need any specific installations. Example:

<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change HTML content.</p>
<button type="button"
onclick="document.getElementById('demo').innerHTML = 'Hello JavaScript!'"
>Click Me!</button>
</body>
</html>
JavaScript string functions can be very useful and can be used to perform operations on strings. String is a primitive value which has no functions and properties because they are not object So JavaScript provides us functionality to play with strings there are various functions you can make actions on strings with.
While working on web pages you need to work with strings and may have requirement to enhance properties of String so JavaScript provides us magic to make string functional so that it can be used to make operations on.
Here, Today I am going to share some top 11 JavaScript string functions.

1.indexOf()

indexOf() string function is used specify where the string value occurs in the string. simply we can say it is used to find a string value in string.
if indexOf() is unable to find the value in string then it return -1.
indexOf() is a case sensitive so it does not find case sensitive word passed in the function. this function can be used in every browser.
var str = "Hello world, find the value akashminds";
var n = str.indexOf("akashminds");
output: 28

2. slice()

JavaScript slice() function is used to extract characters by giving positions as argument , in the first argument we pass position you want to extract character from and second position is till the character you want to extract.
Then it returns extracted part of the string. we often use JavaScript slice function while working with web pages.
Example:
var str = "Hello there this is a slice function";
var res = str.slice(0, 11);
output: Hello there

3.substring() 

substring() function is used to extract some character from string by passing two argument as position in string first and end. it seems similar to slice() But the difference is that is does not include the second argument that is position of last element's value. It does not change the original string.

var str = "Hello coders!";
var res = str.substring(6, 10);
output: code

4.replace()

JavaScript replace() string function is used to replace new string at the place of string to pass to it , replace() function takes two arguments, first the string to you want to replace and second is you want to replace with. we can also define regex to match the string.

const data = 'hello , i need food';
console.log(data.replace('food', 'a doctor'));
output:  "hello , i need a doctor"
Define a regex expression in replace function to match string
const data = 'hello , i need food';
const regex = /food/i;
console.log(data.replace(regex, 'a doctor'));
output:  "hello , i need a doctor"

5.concat()

JavaScript concat() function is used to concatenate two strings then it return a new string. First you add concat() on the string you want to concatenate with in the argument we pass the string we want after it.
concat()  function does not change the existing string but give a new string.

  var str1 = "Hello ";
  var str2 = "world!";
  var res = str1.concat(str2);
  output:  "hello world"

6. trim()

JavaScript  trim() function is used to remove space before of after the string and it does not change the original string but return a new string.

  var str = "       Hello There!        ";
  console.log(str.trim());
  output:  "hello There!"

7. includes()

JavaScript includes() function is used to check weather the string contains the value you pass in the function as argument. includes() function is case sensitive here is an example:

  var str = "Hello there, life is awesome";
  var n = str.includes("life");
  output:  true

8.split()

JavaScript split() function is used to break string or we can say it can split a string into parts by providing an argument in it. the argument of split() function can be a char or regular expression. split() function find the argument value in string then break the string where it gets the passed value to split function. there can be more than on result in the string if it matches then it creates a new array. it does not change the existing value of string. for example:

 var str = "Hello world!";
 var res = str.split(" ");
 output:  ["Hello", "world!"]

9.charAt()

JavaScript charAt() function is used to return character from the string where you pass position as an argument. for example:

 var str = "HELLO THERE!";
 var res = str.charAt(6);
 output:  T

10. toLowerCase()

JavaScript toLowerCase() function is used to convert uppercased string into plain text, this is very useful function we often get to use this most of the places while working on websites.

var str = "Hello World!";
var res = str.toLowerCase();
output:  hello world!

11. toUpperCase() 

JavaScript toUpperCase() function is used to convert a string into lower case even if the string is in lowercase of upper case it will still return uppercased string.

var str = "hello world!";
var res = str.toLowerCase();
output:  Hello World!

Summary

In this Section, we learned how we can perform functions on a string in JavaScript. There can be various kind of  requirements while working on web pages we always have all techniques to change the strings into the format we need so here we had top 11 JavaScript string function which can help us in programming.

No comments:

Powered by Blogger.