Mark As Completed Discussion

String Operations in JavaScript

First string operation to discuss is concatenation. This operation allows us to combine multiple strings together.

JAVASCRIPT
1var book = "The Great Gatsby"
2var author = "F. Scott Fitzgerald"
3
4var sentence = book + "was written by " + author
5
6console.log(sentence) //prints "The Great Gatsby was written by F. Scott Fitzgerald"

Other string operations in JavaScript are defined through string properties. They modify the strings according to the specified operation. Let's see some examples.

  • length property of string gives the length of the string.
  • indexOf property gives the index of the first occurence of the character or a substring in the string.
  • toLowerCase() converts the string to lowercase.
JAVASCRIPT
1var book = "The Great Gatsby"
2var author = "F. Scott Fitzgerald"
3
4console.log(book.length) //prints 16
5console.log(author.indexOf("z")) //prints 12
6console.log(author.indexOf("Scott")) //prints 3
7console.log(author.toLowerCase()) //prints "f. scott fitzgerald"

A comprehensive list of string properties is provided here.