Strings in JavaScript
Published by
sanya sanya
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 -
- 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 -
- substr() - The `substr()` is somewhat similar to
slice(). The code for the same is as follows - - replace() - It replaces a specific portion of the string with the mentioned one. The code for the same is as follows -
- toUpperCase() - It converts the string to upper case. The code for the same is as follows -
- toLowerCase() - It converts the string to Lower case. The code for the same is as follows -
- concat() - It is used to merge two or more strings using
plus(+)operator. The code for the same - - trim() - It is used to remove the whitespaces from the string. The code for the same is as follows -
- charAt() - The
charAt()returns the character at a specified location. The code for the same is as follows -
let numbers = "one, two, three"; let num = numbers.slice(5,6) console.log(num)
//Output: t
let numbers = "one, two, three"; let num = numbers.substr(-4); console.log(num)
//output: hree
let str = "javascript is best"; let my_str = str.replace("best", "awesome") console.log(my_str) //output:
javascript is awesome
let str = "javascript"; let str_up = str.toUpperCase() console.log(str_up) //output:
JAVASCRIPT
let str = "JAVASCRIPT"; let str_up = str.toLowerCase() console.log(str_up)
//output: javascript
let str_1 = "javascript"; let str_2 = "is best"; let str_3 = str_1 + str_2; console.log(str_3);
//output: javascript is best
let str_1 = " javascript "; let str_2 = str_1.trim(); console.log(str_2);
//output: javascript
let str_1 = "javascript"; let str_2 = str_1.charAt(0);
console.log(str_2); //output: j
String Search
Several Javascript String Search are mentioned below -
- indexOf() - The
indexOf()returns the index location of the first occurrence in the string. The code for the same is as follows - - lastIndexOf() - The
lastIndexOf()returns the index value starting to search from backwards. The code for the same is as follows - - search() - The Javascript
search()method is used to use to search a string for a string and return the position of the match. - match() - The
match()method is used to return a matching string against a string. - include() - The
matchAll()method returns an iterator that contains the results of matching a string against a string. - includes() - The
includes()method returns true if a string contains a specific value. The code for the same is as follows - - startsWith() - The startsWith() method returns true if the string begins with the specified value. The code for the same is as follows -
- endsWith() - The endsWith() method returns true if the string ends with the specified value. The code for the same is as follows -
let str = "Javascript is the best among all languages" let index = str.IndexOf("javascript")
//Output: 0
let str = "Javascript is the best among all languages" let index = str.lastIndexOf("javascript")
console.log(index) //Output: 18
let text = "Javascript is the best among all languages"; text.includes("all");
//output: true
let text = "Javascript is the best among all languages"; text.startsWith("Javascript");
//output: true
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

