JavaScript Strings In a Nutshell

“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.

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.

The length property returns the length of a string:

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

  • 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.

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”);

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

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

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

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

--

--

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store