Sitemap
Rajeswari Depala

Rajeswari Depala

Java developer skilled in AI, NLP, and deep learning. Building intelligent solutions and exploring innovations for impactful real-world applications.

Strings in Javascript

2 min readJan 28, 2025

As I said to you before, a string is a sequence of characters enclosed in single quotes (‘’) , double quotes (“ ”) and back ticks(``).

Press enter or click to view image in full size

Strings in JavaScript can be created in three main ways: using string literals, as objects, or with the String() constructor.

Double quotes (") and single quotes (') are interchangeable.

Backticks (`) are used for template literals, allowing string interpolation with ${expression}(a way to directly embed variables within a string).

let strLiteral = "Hello, World!";  // Double quotes
let strLiteral2 = 'Hello, World!'; // Single quotes
let strLiteral3 = `Hello, World!`; // Backticks (template literals)

Template literals allow embedding variables, expressions, or multi-line strings.

let name = 'Alice'
let age = 5
let temp = `Hello ${name} my name is ${age}`
temp
//Prints 'Hello Alice my name is 5'

Strings can be created as objects using the new String() constructor. This creates a String object rather than a primitive string.

new operator lets developers create an instance of a user-defined object type

let strObject = new String("Hello, World!");
console.log(typeof strObject); // "object"

The String() function can also be used to explicitly convert values (numbers, objects, booleans, etc.) into string primitives.

let strFromConstructor = String("Hello, World!");
console.log(typeof strFromConstructor); // "string"

This does not create a String object but a string primitive.

There are two ways to access a character from a string

  1. charAt(index)
  2. Bracket notation
let str = "JavaScript";
console.log(str[0]); // Output: J
console.log(str.charAt(1)); // Output: a

To get the length of a string, we use the .length property to find the length of a string.

let text = "Hello, World!";
console.log(text.length); // Output: 13

And then to cocatenate two strings, we use + operator.

let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName); // Output: John Doe

String Methods

JavaScript strings come with many built-in methods:

Get Rajeswari Depala’s stories in your inbox

Join Medium for free to get updates from this writer.

Changing Case:

let str = "Hello";
console.log(str.toUpperCase()); // Output: HELLO
console.log(str.toLowerCase()); // Output: hello

Searching Strings:

  • .indexOf(): Finds the index of the first occurrence of a substring.
  • .includes(): Checks if a string contains a substring.
let sentence = "JavaScript is fun!";
console.log(sentence.indexOf("fun")); // Output: 15
console.log(sentence.includes("Java")); // Output: true

Extracting Parts of Strings:

  • .slice(): Extracts a section of a string.
  • .substring(): Similar to slice(), but doesn't allow negative indices.
let str = "JavaScript";
console.log(str.slice(0, 4)); // Output: Java
console.log(str.substring(4, 10)); // Output: Script

Replacing Text:

  • .replace(): Replaces a substring with another.
  • .replaceAll(): Replaces all occurrences of a substring (ES2021+).
let text = "I love coding. Coding is fun!";
console.log(text.replace("Coding", "Programming")); // Replaces first occurrence
console.log(text.replaceAll("Coding", "Programming")); // Replaces all

Splitting Strings:

let str = "apple,banana,grape";
console.log(str.split(",")); // Output: ['apple', 'banana', 'grape']

Trimming:

let str = "   Hello!   ";
console.log(str.trim()); // Output: "Hello!"

Happy Coding !

--

--

Rajeswari Depala
Rajeswari Depala

Written by Rajeswari Depala

Java developer skilled in AI, NLP, and deep learning. Building intelligent solutions and exploring innovations for impactful real-world applications.