JavaScript – Create String

To create a string in JavaScript, enclose the string literal in double quotes, single quotes, or back-ticks. Or, we can also use String() constructor.

Examples

The following are some of the quick examples to create a string in JavaScript.

</>
Copy
var str1 = 'hello world';
var str2 = "hello world";
var str3 = `hello world`;
var str4 = new String("hello world");

In the following example, we create a string in JavaScript, and display this string in a div.

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
</head>
<body>
    <div id="output"></div>
    <script>
        var str = 'hello world';
        document.getElementById('output').innerHTML = str;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to create a string in JavaScript, in different ways, with examples.