All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Variable In String

Last Updated : Mar 11, 2024

JavaScript Variable In String

In this article we will show you the solution of JavaScript variable in string, we are going to see the many functions using JavaScript variables in the string. Let us know about some functions used in the example below.

  • charAt(): returns the character of any index
  • concat(): used to join strings
  • startsWith(): if the string starts with that letter return true else return false.
  • startsEnd(): if the string Ends with that letter returns true else returns false.
  • includes(): if the includes that letter returns true else returns false.

Also we are going to using many functions like a.indexOf(), a.lastIndexOf(), a.match(), a.padStart(),a.padEnd(), a.repeat(), a.search(), a.slice(), a.split(), a.substring(), a.toLowerCase(), a.toUpperCase(), a.trim(), a.trimStart(), a.trimEnd().

Step By Step Guide On JavaScript Variable In String :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> JavaScript variable in string </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> JavaScript variable in string </h3>
    </center>
    <script>
        var a = "Talkerscode";
        document.write(a.charAt(4)) ; //e
        document.write("<br>");
        document.write(a.concat(" ","welcome")) ; //Talkerscode welcome
        document.write("<br>");
        document.write(a.startsWith("T")) ; //true
        document.write("<br>");
        document.write(a.endsWith("e")) ; //true
        document.write("<br>");
        document.write(a.includes("x")) ; //false
        document.write("<br>");
        document.write(a.indexOf("e")) ; //4
        document.write("<br>");
        document.write(a.lastIndexOf("l")) ; //2
        document.write("<br>");
        document.write(a.match(/[A-Z]/g)) ; //T
        document.write("<br>");
        document.write(a.padStart(6,"?")) ; //Talkerscode
        document.write("<br>");
        document.write(a.padEnd(6,"?")) ; //Talkerscode
        document.write("<br>");
        document.write(a.repeat(3)) ; //TalkerscodeTalkerscodeTalkerscode
        document.write("<br>");
        document.write(a.search("e")) ; // 4
        document.write("<br>");
        document.write(a.slice(1,3)) ; //al
        document.write("<br>");
        document.write(a.split("")) ; //T,a,l,k,e,r,s,c,o,d,e
        document.write("<br>");
        document.write(a.substring(2,4)) ; //lk
        document.write("<br>");
        document.write(a.toLowerCase()) ; //talkerscode
        document.write("<br>");
        document.write(a.toUpperCase()) ; //TALKERSCODE
        document.write("<br>");
        document.write(a.trim()) ; //Talkerscode
        document.write("<br>");
        document.write(a.trimStart()) ; //Talkerscode
        document.write("<br>");
        document.write(a.trimEnd()) ; //Talkerscode
        document.write("<br>");
    </script>
</body>
</html>
  1. To write HTML code <! DOCTYPE html> to mention which version of the HTML file is written in.
  2. Then we have to write the <html> tag used to define the root of an HTML document. And also write the ending tag </html>.
  3. <head> tag used here to use to contain the metadata about the HTML file and end it with </head> tag
  4. Then <title> tag is used to set the title of the HTML document and end it with </title> tag.
  5. In order to style the HTML page, we'll utilize an external CSS file
  6. using <style> tag to add CSS
  7. <h1> tag used to add a heading close it with </h1> tag
  8. <script> tag is created write JavaScript within it.
  9. First create a variable ‘a’ with var of a string “Talkerscode”.
  10. Using document.write() to display the output of a.charAt(4). <br> to use a line break also.
  11. As similar to above using a.concat(), a.startsWith(), a.endsWith(), a.includes(), a.indexOf(), a.lastIndexOf(), a.match(), a.padStart(),a.padEnd(), a.repeat(), a.search(), a.slice(), a.split(), a.substring(), a.toLowerCase(), a.toUpperCase(), a.trim(), a.trimStart(), a.trimEnd() with document.write()
  12. Using document.write() with <br> tag for line breaks after all the functions.
  13. The output of all the functions is shown in the code comment.

Conclusion :-

Last, here, in conclusion, we can say that with this article’s help, we know How to use variables in string JavaScript.

I hope this article on JavaScript variable in string helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪