String in JavaScript

In this tutorial, we will learn about String in JavaScript. In Strings are normally defined as a sequence of character. For example “Owlbuddy” Yes you can say “Owlbuddy” is a String in the programming language. There are two ways to define String in JavaScript:

  • Using String literal
  • Using String object

Now we will check each way to create and we will try to understand it with the help of examples.

Using String literal:

In this method, we directly write var keyword and then literal for string and then we write a string in double quotes(” “). Follow this example to get a clear idea about this method.

<script>  
var str="Welcome To Owlbuddy";  
document.write(str);  
</script>

Using String object: 

In this method, we create String object using new keyword and then we pass a string in Object. Check the following example.

<script>  
var str=new String("Welcome to Owlbuddy.");  
document.write(str);  
</script>

Basic string Methods:

Here we will check some basic string Methods. That will help you so much while working with Strings

Method Description
charAt() It is use to check char at particular index.
concat() It is use to concat two strings.
indexOf() It is use to find index of character in String.
lastIndexOf() It is use to find lat index of character n string.
search() It is use to search regular expression from string.
replace() It is use to replace part of string with some other char squence
substr() It is use to get sub string from big string.
toLowerCase() It converts whole string into lower case letters.
toUpperCase() It converts whole string into upper case letters.
toString() It return string form an object.
valueOf() Provide primitive value of String object.
Spread the love
Scroll to Top
×