javascript
  1. javascript-string

JavaScript String

A string in JavaScript is a sequence of characters, enclosed within quotes. In JavaScript, strings can be of two types: primitive and object.

Syntax

The syntax for creating a string in JavaScript is as follows:

var str = "Hello, world!";

The above syntax declares a string variable str with the value "Hello, world!".

Example

Let's take an example to demonstrate the usage of strings in JavaScript:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript String Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Define variables
    var name = "John Doe";
    var greeting = "Hello";
    var message = greeting + " " + name;

    // Display the message in the HTML document
    document.getElementById("output").innerHTML = "Output: " + message;
</script>

</body>
</html>

In the above example, we have concatenated two strings greeting and name using the + operator and assigned the result to the message variable. The console.log() method is used to print the output, which will be "Hello John Doe".

Output

The output of the above example will be:

Hello John Doe

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript String Example</title>
</head>
<body>

<!-- Placeholder for displaying the output -->
<p id="output"></p>

<script>
    // Define variables
    var name = "John Doe";
    var greeting = "Hello";
    var message = greeting + " " + name;

    // Display the message in the HTML document
    document.getElementById("output").innerHTML =  message;
</script>

</body>
</html>
Try Playground

Explanation

In JavaScript, strings can be surrounded by single or double quotes. The + operator is used for string concatenation and the length property is used to determine the length of a string.

Moreover, string literals can be concatenated together using the + operator, as shown in the example above. Additionally, strings can be accessed via zero-based index, meaning that the first character is at index 0 and so on.

Use

Strings are commonly used for storing and manipulating text data in JavaScript. Some common uses of strings include:

  • Storing user input
  • Adding text to HTML elements
  • Formatting output data
  • Manipulating and parsing data

Important Points

  • Strings in JavaScript are immutable, meaning that the value of a string cannot be changed once it is created.
  • JavaScript has a number of built-in methods for manipulating strings, including indexOf(), slice(), concat(), and many more.
  • String methods always return new strings and do not modify the original string.

Summary

In summary, JavaScript strings are a fundamental data type used for storing and manipulating text in JavaScript. They are created using quotes, can be concatenated using the + operator, and provide a wide range of built-in methods for manipulating string data.

Published on: