Skip to content

JavaScript Tutorial Strings

JavaScript Strings: Creating, Length and Characters

Lesson 29 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

A string is text. You write it between quotes.

script.js
JS
const single = 'Hello';
const double = "Hello";
const backtick = `Hello`;

console.log(single === double);   // → true
console.log(double === backtick); // → true

Single and double quotes work the same. Use one style consistently. Backticks create a template literal, which has extra powers: see the next lesson, Template Literals.

Quotes inside strings

If the text contains the same quote you used, either switch the outer quote or escape the inner one with a backslash.

script.js
JS
console.log("She said \"hi\"");  // → She said "hi"
console.log('It\'s fine');        // → It's fine
console.log("It's fine");         // → It's fine (no escape needed)

Common escapes: \n (new line), \t (tab) and \\ (a backslash).

script.js
JS
console.log("Line one\nLine two");

Length

.length gives the number of characters (strictly, UTF-16 code units).

script.js
JS
const word = "JavaScript";
console.log(word.length); // → 10
console.log("".length);   // → 0

Accessing characters

Positions start at 0.

script.js
JS
const word = "Cat";
console.log(word[0]);        // → C
console.log(word[2]);        // → t
console.log(word.at(-1));    // → t   (count from the end)
console.log(word[10]);       // → undefined

.at(-1) is a modern way to read from the end. The older way is word[word.length - 1].

Strings are immutable

You cannot change a character in place. Methods return a new string.

script.js
JS
const word = "cat";
console.log(word.toUpperCase()); // → CAT
console.log(word);               // → cat (unchanged)
info
A note about emoji
Some characters, such as many emoji, take two code units. "😀".length is 2. For most text this does not matter, but keep it in mind if you count characters for display.