Basic Programming Tutorials

JavaScript String

Welcome to the JavaScript tutorial, we have already covered JavaScript Boolean, JavaScript number and objects. Today we will talk about the JavaScript string object. The JavaScript string object can be created by using string constructor. The JavaScript has helper methods for string object to wrap the primitive data types.

JavaScript String object – Syntax

Now we will see the syntax for creating a JavaScript string object:

Var sharp = new String (“sharpleson”);

Here, ‘sharp’ is an object, and ‘sharplesson’ is a string. You can have any name in place of ‘sharp’ and strings in place of ‘sharplesson’.

Note: We have shown you the practical example, instead of showing the copybook syntax. Because, example is better to understand.


JavaScript string object – Properties

Now we will see the common JavaScript string properties that you will use. For sure constructor is common for many objects.

  • Constructor () – Again, it will return the function which made the object.
  • prototype () – If you want to add methods or any properties to the string object, then you can use it.
  • length () – It will let you know the number of elements in a string.

JavaScript String object – Methods

Now we will see the common JavaScript string methods that you will use.

  • concat () – It means joining, you can use it, if you want to join two strings. It will join it and then return the final result.
  • charAt () – You can use it, if you wish to get specific characters from the string. Just mention the index number, and you will get the character that has got the mentioned index.
  • charCodeAt() – You can use it to find the Unicode of a character in the mentioned index.
  • indexOf () – It will return the index of the character or a string, in the very first occurrence. In case the value will be not found, then it will return -1. Like JavaScript, It is case sensitive too.
  • match () – You can use this method to match string against the regular expression. Once found, it will return it in the array object. In case there is no match, then it will not return anything, It means null.
  • replace () – You can use it to replace the string. It will search for the matched string, and it will return the new replaced string.
  • search () – It will search for the specific character or string, and returns the index value of that string, or character. Note: if no match found, then it will return -1.
  • substr () – You can use this method to get a specific part of the string. You just mention the starting position of the string and closing position as well. Now it will only return the specific characters at the specified position. Note: If you will not mention the closing position, then it will return the entire string after the mentioned starting position.
  • toLowerCase () – It will simply convert the string to lower case.
  • toUpperCase () – It is opposite to that lowercase, It will convert the string into uppercase.
  • toString () – It will return the value of the string object.
  • trim ()  – You can use it to remove the white spaces from both sides of the string.

Note: We have mentioned the most used methods in the JavaScript string only. If you want the full list, then do refer our JavaScript reference.

JavaScript String object – Example

Now we will see the use of methods to understand the use of the JavaScript string objects. In the below example, we have used ‘toLowerCase (), substr () and toUpperCase ()’ methods. Now let’s see what will be the output of the below code:

<html>

<body>

<script type="text/javascript">

var sharp=("Hello sharplesson readers!")

document.write(sharp.toLowerCase() +"<br>")

document.write(sharp.substr(0,3) + "<br>")

document.write(sharp.toUpperCase())

</script>

</body>

</html>

The output will be:

hello sharplesson readers!
Hel
HELLO SHARPLESSON READERS!

We can clearly see that the first line is showing the string in a lower case. The second line shows the specific part that we mentioned, that is from 0 to 3. The last part is showing the string in the Upper case.

Conclusion

We have just covered the JavaScript string object, its syntax, uses and methods. We have shown you, how to use methods as well. It is very simple to understand that the JavaScript is a beautiful language to know. If you will get perfection in it, then nobody will be able to catch your hand.