How to declare String Variables in JavaScript?


JavaScript is an untyped language. This means that a JavaScript variable can hold a value of any data type. To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.

Here’s how you can declare strings in JavaScript -

var name = "David";
var subject = "Programming";

Example

You can try to run the following code to learn how to declare strings in JavaScript

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var re = "java";
         var str = "Learn java";
         if ( str.search(re) == -1 ){
            document.write("Not found!" );
         } else {
            document.write("Found!" );
         }
      </script>
   </body>
</html>

Output

This will produce the following result -

Found!

In the above program, we search string 'java' in another string 'Learn java'.

Handling Quotes

Sometimes a string contains quotes (single and/ or double) in itself so JavaScript may misunderstand it as shown in the example below −

var str = "In elementary school, we learn "basic skills" like reading and writing";

Example

See the complete example below −

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var str1 = 'It is 5 o' clock in the morning.';
         var str2 = "In elementary school, we learn "basic skills" like reading and writing";
         document.write(str1 + "</br>" );
         document.write(str2 + "</br>");
      </script>
   </body>
</html>

Output

The compiler will show syntax error in printing the above statement due to abrupt use of quotes.

Uncaught SyntaxError: Unexpected identifier (at index.html:8:53)

So to print these types of statements we have to use the quotes with backslash(\) like shown below −

var sentence = “In elementary school we learn \“basic skills\” like reading and writing”;

Example

Now have a look at the complete example below -

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var str1 = 'It is 5 o'clock in the morning.';
         var str2 = "In elementary school, we learn "basic skills" like reading and writing";
         document.write(str1 + "</br>" );
         document.write(str2 + "</br>");
      </script>
   </body>
</html>

Output

It will produce the following result

It is 5 o’clock in the morning.
In elementary school, we learn "basic skills" like reading and writing

Strings as Object

In JavaScript the strings can also be defined as objects using the keyword "new" like it is shown below

var obj = new String("Tutorials Point");

Example

In the example below, we define a string variable as an object.

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var obj = new String("Welcome to Tutorials Point");
         document.write(obj);
      </script>
   </body>
</html>

Output

This will produce the following result -

Welcome to Tutorials Point

Strings in ES6 JavaScript

ES6 introduced template literals that allow you to define a string using backtick(`) characters as shown below −

var str = `In elementary school we learn "basic skills" like reading and writing`;

Example

In the example below, we define a string variable using the backtick.

<html>
   <head>
      <title>JavaScript Strings</title>
   </head>
   <body>
      <script>
         var str1 = `It is 5 o'clock in the morning.`;
         var str2 = `In elementary school, we learn "basic skills" like reading and writing`;
         document.write(str1 + "</br>" );
         document.write(str2 + "</br>");
      </script>
   </body>
</html>

Output

This will produce the following result -

It is 5 o'clock in the morning.
In elementary school, we learn "basic skills" like reading and writing

This new template solves the problem of using single or double quotes inside the string with this backtick option one can use any special character inside a string without using any backslash.

Ali
Ali

Updated on: 14-Sep-2023

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements