Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Strings in JavaScript

User image

Published by

sanya sanya

Published at: 6th May, 2023
3.48 mins read

The collection of Characters is known as String. The strings can be declared using single quotes or double quotes. For instance:

let str = "Javascript is a good language;

We can extract the length of the string in Javascript by using the code below -

let str = "Javascript is a good language"; let len = str.length; console.log(len);
//Output: 29 (spaces included)

Javascript Strings Escape Sequence

Various Javascript sequences are mentioned below -

  • /b - Backspace
  • /n - New Line
  • /f - Form Feed
  • /r - Carriage Return
  • /t - Horizontal Tabular
  • /v - Vertical Tabular

Javascript String Methods

Several Javascript String Methods supported by Javascript are mentioned below -

  1. Slice - The `slice()` extracts a portion of the string and returns the extracted portion of the string. The code for the same is given below -

  2. let numbers = "one, two, three"; let num = numbers.slice(5,6) console.log(num)
    //Output: t

  3. substr() - The `substr()` is somewhat similar to slice(). The code for the same is as follows -
  4. let numbers = "one, two, three"; let num = numbers.substr(-4); console.log(num)
    //output: hree

  5. replace() - It replaces a specific portion of the string with the mentioned one. The code for the same is as follows -

  6. let str = "javascript is best"; let my_str = str.replace("best", "awesome") console.log(my_str) //output:
    javascript is awesome

  7. toUpperCase() - It converts the string to upper case. The code for the same is as follows -

  8. let str = "javascript"; let str_up = str.toUpperCase() console.log(str_up) //output:
    JAVASCRIPT

  9. toLowerCase() - It converts the string to Lower case. The code for the same is as follows -
  10. let str = "JAVASCRIPT"; let str_up = str.toLowerCase() console.log(str_up)
    //output: javascript

  11. concat() - It is used to merge two or more strings using plus(+) operator. The code for the same -
  12. let str_1 = "javascript"; let str_2 = "is best"; let str_3 = str_1 + str_2; console.log(str_3);
    //output: javascript is best

  13. trim() - It is used to remove the whitespaces from the string. The code for the same is as follows -
  14. let str_1 = " javascript "; let str_2 = str_1.trim(); console.log(str_2);
    //output: javascript

  15. charAt() - The charAt() returns the character at a specified location. The code for the same is as follows -
  16. let str_1 = "javascript"; let str_2 = str_1.charAt(0);
    console.log(str_2); //output: j


Several Javascript String Search are mentioned below -

  1. indexOf() - The indexOf()returns the index location of the first occurrence in the string. The code for the same is as follows -
  2. let str = "Javascript is the best among all languages" let index = str.IndexOf("javascript")
    //Output: 0

  3. lastIndexOf() - The lastIndexOf() returns the index value starting to search from backwards. The code for the same is as follows -
  4. let str = "Javascript is the best among all languages" let index = str.lastIndexOf("javascript")
    console.log(index) //Output: 18

  5. search() - The Javascript search() method is used to use to search a string for a string and return the position of the match.

  6. match() - The match()method is used to return a matching string against a string.

  7. include() - The matchAll() method returns an iterator that contains the results of matching a string against a string.

  8. includes() - The includes() method returns true if a string contains a specific value. The code for the same is as follows -
  9. let text = "Javascript is the best among all languages"; text.includes("all");
    //output: true

  10. startsWith() - The startsWith() method returns true if the string begins with the specified value. The code for the same is as follows -

  11. let text = "Javascript is the best among all languages"; text.startsWith("Javascript");
    //output: true

  12. endsWith() - The endsWith() method returns true if the string ends with the specified value. The code for the same is as follows -

  13. let text = "Javascript is the best among all languages"; text.startsWith("languages");
    //output: languages

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

Javascript Strings Escape Sequence

Javascript String Methods

String Search