JavaScript Strings In a Nutshell

Adarsh gupta
2 min readJan 15, 2022

“something between double/single quote”

source Unsplash

JavaScript string is a primitive data type that is used to work with texts.

Different ways to include strings in Javascript.

Strings are usually created by surrounding them with quotes.

1. Single quotes.

2. Double quotes.

3) Backticks.

Single and Double quotes are just the same in case of meaning.

But Backticks are used when we need to include a variable or an expression in a string.

const Name='Adarsh'

console.log(`Hello my name is ${Name}`)

This will print Hello my name is Adarsh.

Let's now look into some String methods.

1) Length

The length property returns the length of a string:

let txt = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
let length = txt.length;

2) Extracting strings

  • slice(start, end)
  • substring(start, end)
  • substr(start, length)

slice() extracts a part of a string and returns the extracted part in a new string.

substring() is similar to slice().

The difference is that substring() cannot accept negative indexes.

substr() is similar to slice().

The difference is that the second parameter specifies the length of the extracted part.

3)Replace Method

The replace() method replaces a specified value with another value in a string:

let text = “You can learn something from youtube!”;
let newText = text.replace(“youtube”, “medium”);

4)Upper and Lower Case

A string is converted to upper case with toUpperCase():

A string is converted to lower case with toLowerCase():

5) charAt

The charAt() method returns the character at a specified index (position) in a string:

There are way more string methods. You can check the string methods here: String Methods

Conclusion

We discussed the different ways in which we can declare a strings and few methods of strings in JavaScript.Hope you learned something.

Keep Learning Keep Updated

--

--

Adarsh gupta

Software Engineer | JavaScript developer | Technical Writer . Work with me? adarshguptaworks@gmail.com Connect with me? twitter.com/adarsh____gupta/