Interesting Facts About JavaScript Strings
Last Updated :
13 Nov, 2024
Let's see some interesting facts about JavaScript strings that can help you become an efficient programmer.
Strings are Immutable
In JavaScript, strings are immutable, which means once you create a string, you cannot change its characters individually. Any operation that appears to modify a string, like replace() or concat(), actually creates a new string.
JavaScript
let s = "hello";
s[0] = "H"; // Won't work
console.log(s); // Still "hello"
Output
"hello"
Using Template Literals
Template literals, introduced in ES6, allow for writing expressions directly into strings using backticks ` and ${} syntax.
JavaScript
let s1 = "Sourav";
let s2 = `Hi, ${a}!`;
console.log(s2);
Output
"Hi, Sourav!"
String Comparison
JavaScript compares strings letter by letter. This makes JavaScript strings different from some languages that use locale-based comparisons.
JavaScript
console.log("apple" > "banana");
Output
false
Strings as Arrays of Characters
We can access JavaScript strings just like as in JavScript Arrays to get the individual character.
JavaScript
let s = "hello";
console.log(s[1]);
Output
"e"
String Methods Create Copies
When you call a method like toUpperCase() or slice(), it returns a modified copy of the original string. The original string remains unaffected.
JavaScript
let s1 = "hello";
let s2 = s1.toUpperCase();
console.log(s2);
console.log(s1);
Output
"HELLO"
"hello"
Splitting and Joining Strings
The split() method allows you to break a string into an array, and join() does the opposite by joining elements of an array into a single string.
JavaScript
let s1 = "apple,banana";
let s2 = s1.split(",");
console.log(s2);
console.log(s2.join(" & "));
Output
["apple", "banana"]
"apple & banana"
Add Padding with padStart() and padEnd()
ES2017 introduced padStart() and padEnd() by which we can add padding characters to the beginning or end.
JavaScript
let s = "5";
console.log(s.padStart(3, "0"));
console.log(s.padEnd(3, "-"));
Output
"005"
"5--"
Reversing a String
Although JavaScript doesn’t have a built-in reverse method for strings, you can reverse a string by combining split(), reverse(), and join() methods.
JavaScript
let s1 = "hello";
let s2 = s1.split("").reverse().join("");
console.log(s2);
Output
"olleh"
String Length as a Property
Unlike arrays, you can get the length of a string directly with .length.
JavaScript
let s = "hello";
console.log(s.length);
Output
5
Working with Multi-Line Strings
Template literals allow to write multi-line strings without using escape characters (\n).
JavaScript
let s = `Roses are red,
Violets are blue,
JavaScript is fun,
And so are you!`;
console.log(s);
Output
Roses are red,
Violets are blue,
JavaScript is fun,
And so are you!
Check for Substrings Easily
JavaScript provides includes(), startsWith(), and endsWith() methods, which makes easy to check substrings.
JavaScript
let s = "JavaScript is great";
console.log(s.includes("JavaScript"));
console.log(s.startsWith("Java"));
console.log(s.endsWith("great"));
Output
true
true
true
Similar Reads
Interesting Facts About JavaScript JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It is an interpreted, high-level programming language that follows ECMAScript. It powers interactive websites and is packed with amazing features that make it special and powerful. Interesting Facts A
5 min read
JavaScript - Insert Character in JS String In JavaScript, characters can be inserted at the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.At the BeginningTo insert a character at the beginning of a string, you can use string concatenation or template literals
1 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read
Compressing String in JavaScript Compressing a string involves replacing repeated characters with their count followed by the character itself. For example, string aaabbccc can be compressed to 3a2b3c. Examples:Input: string = aaabbbcccOutput: compressed string = 3a3b3c Input: string = heeeeellllllooooooOutput: compressed string =
2 min read
JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav
15+ min read