Table of contents
1.
Introduction
2.
Create Strings in JavaScript
2.1.
1. By Using String Literal
2.2.
2. By Using String Object (New Keyword)
3.
Javascript String Methods
4.
JavaScript Methods with Code
4.1.
1. JavaScript String Length Method 
4.2.
2. JavaScript String ChartAt Method  
4.3.
3. JavaScript String IndexOf() Method
4.4.
4. JavaScript String Concat() Method 
4.5.
5. JavaScript String CharCodeAt() Method 
4.6.
6. JavaScript String Includes() Method 
4.7.
7. JavaScript String LastIndex() Method
4.8.
8. JavaScript String Replace() Method
4.9.
9. JavaScript String Match() Method
4.10.
10. JavaScript String EndsWith() Method 
4.11.
11. JavaScript String Slice() Method 
4.12.
12. JavaScript String Substr() Method 
4.13.
13. JavaScript String Substring() Method
4.14.
14. JavaScript String toLowerCase() Method
4.15.
15. JavaScript String toUpperCase() Method 
4.16.
16. JavaScript String trim() Method 
4.17.
17. JavaScript trim End() Method 
4.18.
18. JavaScript pad Start() Method 
4.19.
19. JavaScript pad End() Method 
5.
Frequently Asked Questions
5.1.
Q. What is a string function in JavaScript?
5.2.
Q. What does string () do?
5.3.
Q. How do you write a string function in JavaScript?
5.4.
Q. How do you declare a string function?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript String() Method

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about String Functions in javascript. We will see different methods that are present in the Strings. We will see how Strings are created in javascript. We will look at how these methods and functions are used in the code.

String() Functions in JavaScript

Strings are sequences of characters that are used for holding the data in the text form. In general, strings don’t have methods and functions, but in javascript, strings are treated as objects, so strings have methods and functions in javascript. We can manipulate the Strings according to their needs.

Create Strings in JavaScript

There are two ways in which strings are created in javascript.

1.  Using string literal

2.  Using string objects i.e using a new keyword

1. By Using String Literal

In string literal, we try to use double inverted commas to create a new string.

Syntax:

var msg=”I am string literal”;

Example:

<script>  
var msg="This is how we use string literal";  
document.write(str);  
</script> 


Output

This is how we use string literal

2. By Using String Object (New Keyword)

To create a new string object, we use new keyword.

Syntax:

var stringName=new String("New String is created");  

Let’s see this with the example.

Example:

<script>  
var stringName=new String("New String is created");  
document.write(stringName);  
</script>  


Output

New String is created


Also check out - Substr C++

Javascript String Methods

Methods 

Description

length This method returns the length of the String.
charAt() This returns the character present at the index in the string.
indexOf() This returns the index position of the given input from the string.
concat() It combines two or more strings and returns a new string.
charCodeAt() At the mentioned index, it returns the unicode value of the character present. 
includes() This determines you to find out the given string in the searched string.
lastIndexOf() The lastIndexOf() method gives the index of the last occurrence of the given string.
replace() It replaces the string with the specified string.
match() It searches for the specified string in the string and returns the specified string in case it matches the expression.
endsWith() It determines whether the strings end with the mentioned string.
slice() It takes the start and end as parameters and returns the string from the starting till the end-1.
substr() It returns the string from the specified position to the length mentioned in the second parameter.
substring() It fetches the string from the starting position mentioned to the length given.
toLowerCase() It converts the string to lowercase.
toUpperCase() It converts the string to uppercase.
trim() It trims the white space from the beginning and end of the string.
trimStart() It trims the white space from the beginning of the string.
trimEnd() It removes the whitespace from the ending of the string.
padStart() It pads the present string from starting with the current string and returns the new string of the specified length.
padEnd() It pads the present string from the end with the current string and returns the new string of the specified length.


Check out these article - String slicing in Python  and Fibonacci Series in JavaScript

JavaScript Methods with Code

1. JavaScript String Length Method 

It is used to get the length of the string.

Example:

<script>
let msg = ”String Functions in javascript”;
let length= msg.length;
document.write(length);
</script>


Output

30

2. JavaScript String ChartAt Method  

This method returns the character present at that index. Indexing in strings also starts with 0.

Example:

<script>
    var str=”String Functions in javascript”;  
    document.write(str.charAt(2));       
    </script>


Output

r

3. JavaScript String IndexOf() Method

This returns the index position of the given input from the string.

Example:

<script>  
var msg="Learn String Functions in javascript";  
var n=msg.indexOf("String");  
document.write(n);  
</script>  


Output

7

4. JavaScript String Concat() Method 

It combines two or more strings and returns the new string.

Example:

<script>  
  var x="Coding ";  
  var y="Ninjas";  
   document.writeln(x.concat(y));  
</script> 


Output

Coding Ninjas

5. JavaScript String CharCodeAt() Method 

It returns the Unicode value of the character present at the mentioned index.

Example:

<script>  
   var x="String Functions in javascript";  
   document.writeln(x.charCodeAt(3));  
</script>  


Output

105

6. JavaScript String Includes() Method 

It returns true if the searched string is included in the given string.

Example:

let sentence = 'Coding Ninjas.';

let word = 'Ninjas';

console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);


Output

True

7. JavaScript String LastIndex() Method

It searches for the last occurrence of the word in the string. It returns the index of the last occurrence of the word. It returns -1 in case the string is not found.

Example:

<script>
  let  msg=”Hello, hi, Hello”;
  let search= msg.lastIndexOf(“Hello”);
  document.write(search);
</script>


Output

12

8. JavaScript String Replace() Method

It searches for the given text to be replaced in the given string. It takes two parameters. The string to be replaced,  the second is a new string to be added in the replacement of the first one. In return, it returns the new string with the string that is replaced.

Example:

<script>
  let  msg=”Hello, hi, Hello”;
  let newString= msg.replace(‘Hi’,’Hey’);
  document.write(newString);
</script


Output

Hello, Hey, Hello

9. JavaScript String Match() Method

It takes one argument as a parameter, returns the string if found, and in case the string does not match, it returns null. If the given value is in the string, it converts it to a regular expression.

When the match method has a string as an argument

Example:

<script>  
var x="Coding Ninjas ";  
var y= x.match(“Ninjas”);
document.write(y);
</script> 


Output

Ninjas


Using regular expression

Example:

<script>  
  var x="Coding Ninjas ";  
  var y= x.match(/Ninjas/);
  document.write(y);
</script> 


Output

Ninjas

10. JavaScript String EndsWith() Method 

It matches the given string ends with the specified sequence of characters given in the parameter. It is a case-sensitive method. It returns true or false as output.

Example:

<script>  
  var x=’Coding Ninjas’;  
  var y= x.endsWith(‘njas’);
  document.write(y);
</script> 


Output

True

11. JavaScript String Slice() Method 

This method extracts a part of the string and returns the extracted part to a new string. In the case of an array, it returns a new array. It has different methods depending upon the parameters.

  • slice()
  • slice(start)
  • slice(start,end)

slice()

If no value is given in the parameters, it starts from 0.

Example:

If no value is given in the parameters, it starts from 0.

<script>
  let  message=’String functions in javascript.’;
  let result=message.slice();
  document.write(result);
</script>


Output

String functions in javascript.


slice(start)

It starts from the start parameter till the end, and returns the new string.

Example:

<script>
  let  message='I am studying String functions in javascript.’;
  let result=message.slice(14);
  document.write(result);
</script>


Output

String functions in javascript


slice(start,end)

This method of slice takes two parameters the start and the end. It returns the string or new array without including the end character.

Example:

<script>
  const numbers=[ 13, 56, 75, 61, 78, 97, 45]; 
  const newNumbers= numbers.slice(2,5);
  document.write(newNumbers);
</script>


Output

75 61 78

12. JavaScript String Substr() Method 

It can have two parameters. The start and the length. The length parameter is optional. It returns the new string from the start to the length. The method does not make any changes in the original string.

Example:

<script>
  let  message=’I am studying String functions in javascript.’;
  let result=message.substr(13);
  document.write(result);
</script>


Output

String functions in javascript.

13. JavaScript String Substring() Method

It returns the substring of the original string. It does not make any changes in the original string. In case the start is greater than the end, it automatically swaps both values.

Example:

<script>
  let  message=’I study from Coding Ninjas.’;
  let result=message.substring(14,28);
  document.write(result);
</script>


Output

Coding Ninjas

14. JavaScript String toLowerCase() Method

It convert the string to lowercase characters. This method does not make any changes in the original string. Returns the string in the lowercase.

Example:

<script>
  let  message=’CODING NINJAS’;
  let result=message.toLowerCase();
  document.write(result);
</script>


Output

coding ninjas

15. JavaScript String toUpperCase() Method 

It convert the string to uppercase characters. This method does not make any changes in the original string. Returns the string in the uppercase.

Example:

<script>
  let  message=’coding ninjas’
  let result=message.toUpperCase();
  document.write(result);
</script>


Output

CODING NINJAS

16. JavaScript String trim() Method 

It removes the whitespace from both the ends of the string i.e beginning and the end.

Example:

<script>
  let string=”     Hi, This is Rudra!     ”;
  let result = string.trim();
  document.write(result);
</script>


Output

Hi, This is Rudra!

17. JavaScript trim End() Method 

It trims the white space from the end of the string. It does not make any changes in the original string.

Example:

<script>
  let string=  ‘    Hi, This is Rudra!    ‘;
  let result = string.trimStart();
  document.write(result);
</script>


Output

‘Hi, This is Rudra!    ‘

18. JavaScript pad Start() Method 

It pads the present string from starting with the current string and returns the new string of the specified length

padStart(totalLength,padString)

Example:

<script>
  let string=‘    Hi, This is Rudra!    ‘;
  let result = string.trimEnd();
document.write(result);
</script>


Output

‘    Hi, This is Rudra!’  

19. JavaScript pad End() Method 

It pads the present string from the end with the current string and returns the new string of the specified length.

padEnd(totalLength,padString)

Example:

<script>
  var number=99;
  var result= number.padStart(5, 0);
  document.write(result);
</script>


Output

00099


You can also practice on an online javascript editor.

Must Read Difference Between "var", "let" and "const" in JS

Frequently Asked Questions

Q. What is a string function in JavaScript?

String function in javascript is a function which takes string as input and gives modified string as output. For example toUpperCase(), toLowerCase() and trim().

Q. What does string () do?

String() is a constructor function in JavaScript that is used to generate a new string object from a specified value, such as a number or another data type, by converting it to a string.

Q. How do you write a string function in JavaScript?

A string function in JavaScript is created by declaring a function, receiving string parameters, performing specified actions within the function, and then returning a string value.

Q. How do you declare a string function?

Declare a string function in JavaScript by defining a named function, providing parameters, and returning the appropriate string result based on the input parameters and logic within the function.

Conclusion

In this blog, we have discussed string Functions in JavaScript. We have looked at different methods and properties of string functions in javascript. Learn more about strings in javascript, by learning from the blogs below:

JS Strings

Learn Javascript

Introduction to Javascript

Recommended problems -

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Live masterclass