String

Introduction

Strings hold a sequence of characters and a string is defined by denoting its constituent characters.

. . . = new String( . . . )

Example

The following is the code for defining a string.

var s1 = "TreeLeaves";

On running the above code, the following output is obtained.

String

There is only one string operator. a + b : string concatenation

Example

var x = "Welcome to " + "TreeLeaves!"; document.write(x);

On running the above code, the following output is obtained.

Concatenation

document.write("Tree " + "Leaves");

On running the above code, the following output is obtained.

Character Replacement

A character can be replaced by another character within a string.

Example

The following is the code for replacing a character by another within a string.

document.write("This replaces e with a " + s1.replace('e', 'a'));

On running the above code, the following output is obtained.