JavaScript Strings

The JavaScript string is an object that represents a sequence of characters. These characters could be letters, numbers, special characters. Strings are created by enclosing the string characters either within single quotes ' or double quotes ". Whatever you write inside these quotes will be considered as a string and will get printed as it is. You can use any digit, alphabet, or any symbol in a string.


JavaScript Escape Sequences

As, a web browser ignores the whitespaces, what if we willingly wanted to have whitespace between text. How would we able to display those spaces without getting ignored by the browser ?

Escape characters helps us to display whitespace in the browser and also apart from that, provide many other helpful characters. Escape sequences using backslash(\) are very useful for displaying those characters that can't be typed using a keyboard.

Here are some most commonly used escape sequences:

  • \n is used for newline character. It breaks the flow of text and then displays the text in next line.
  • \t is used for tab character. It displays multiple whitespaces as of length of a tab character.
  • \r is used for carriage-return character. It moves the cursor to the beginning of the line.
  • \b is used for backspace character.
  • \\ is used for a single backslash (\)

There are 2 ways to construct string in JavaScript:

  • Using an String literal.
  • Using the keyword new.

JavaScript String Literal

The strings you write in the source code are written in quotes or backticks, but browser output does not include any quotations. So, A string literal is what you write with quotation in the source code and what we see in the output is called string value.

Syntax:

var string_name="string value";


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String </title>
</head>
<body>
<script>
var str="This is an Example of string literal";
document.write(str);
</script>
</body>
</html>

Output

JavaScript String Examples

JavaScript String new Keyword

The new keyword is used to create instance of string. That instance can be used anywhere in the program as a reference to the string.

Syntax
var string_name = new String("string literal");


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript String New Keyword </title>
</head>
<body>
<script>
var stringname = new String("This is an Example of javascript string");
document.write(stringname);
</script>
</body>
</html>

Output

JavaScript String New Keyword Examples



Color Picker

List of color names supported by all browser »



Character Code

List of character codes to display special character »



Language Code

List of all language supported by all browser »



Country Code

List of Country code supported by all browser »

Latest Updates

PHP Introduction

How to Run a PHP program in Xampp?

How can I create a phpinfo.php page?

How to Define a Variable in PHP

PHP Echo and Print Statements

Working with PHP Operators

Understanding the PHP Data Types

How to Write Comments in PHP

How to Define a Constant in PHP

PHP if...else...elseif Statements