Open In App

What are the builtin strings in JavaScript ?

A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (‘) or within double quotes (“). 

Syntax:



var myString = 'Good Morning123!'; // Single quoted string
var myString = "Good Morning123!"; // Double quoted string

In Javascript, many string methods are either built-in or user-defined. Built-in string methods are the methods that are present in any programming language library

Built-in string methods of JavaScript:



Below are some examples demonstrating the use of the Javascript string methods.

Example: This example shows the use of the Javascript string search() method. 




<p id="demo"></p>
  
<script type="text/javascript" charset="utf-8">
    function myFunction() {
    var str = "Welcome to GeeksforGeeks!";
    var a= str.search("GeeksforGeeks");
    document.getElementById("demo").innerHTML = a;
    }
    myFunction()
</script>

Output:

11

Example: This example shows the use of the Javascript string split() method. 




<p id="demo"></p>
  
<script type="text/javascript" charset="utf-8">
    function myFunction() {
      var str = "How are you feeling today?";
      var res = str.split(" ");
      document.getElementById("demo").innerHTML = res;
    }
    myFunction()
</script>

Output:

How, are, you, feeling, today?

Example: This example shows the use of the Javascript string startsWith() method. 




<p id="demo"></p>
  
<script type="text/javascript" charset="utf-8">
    function myFunction() {
      var str = "Hello world, welcome to the universe.";
      var n = str.startsWith("Hello");
      document.getElementById("demo").innerHTML = n;
    }
    myFunction()
</script>

Output:

true

Article Tags :