What is String ?
- A string is a sequence of characters.
- It is a primitive data type.
String can be created using three different types of delimiters.
Single Quotes - 'Hello'
Double Quotes - "Hello"
Backticks -
Hello-(Template Literals) Using this, we can embed variables or any valid expression directly inside the string using ${expression} syntax. Templates allow single and double quotes inside a string.
let name = "Ammu";
let message = `Hello ${name}`;
console.log(message);
let message = `Hello "Varun"`;
console.log(message);
Ouput:
Hello Ammu
Hello "Varun"
Access Characters :
let name = "Hello";
console.log(name[0]);
console.log(name[1]);
console.log(name[2]);
console.log(name[3]);
console.log(name[4]);
console.log(name[5]);
Output :
H
e
l
l
o
undefined
Strings are Immutable:
- Once a string is created, its value cannot be changed.
- Bracket Notation Fails: cannot change a single character using its index.
- Methods Return New Strings: Every string method (like .toUpperCase(), .slice(), or .replace()) returns a brand-new string rather than modifying the one you called it on.
let name = "Hello";
console.log(name.toUpperCase());
console.log(name);
Output :
HELLO
Hello
The Value is Immutable: The characters "Hello" in memory can never be changed to "Gello".
The Variable is Mutable: If you use let or var, you can point that variable to a new string at any time.
let name = "Hello";
name[0] = "G"
console.log(name); //Hello
name = "Hi";
console.log(name); //Hi
Ouput :
Hello
Hi
String Length :
console.log("JavaScript".length);
Output :
10
Common String Methods :
toUpperCase()/toLowerCase() :
let str = "Hello";
console.log(str.toUpperCase()); // HELLO
console.log(str.toLowerCase()); // hello
trim():
- This method removes whitespace from both sides of a string.
let str = " hello ";
console.log(str.length); //7
console.log(str.trim().length); //5
includes() :
-This method determines whether one string can be found within another string, returning a boolean.
- The search is case-sensitive.
let str = "JavaScript";
console.log(str.includes("Script")); // true
console.log(str.includes("script")); // false
console.log(str.toLowerCase().includes("script")); // true
indexOf() :
let str = "JavaScript";
console.log(str.indexOf("a")); //1
console.log(str.indexOf("a",2)); //3
slice() :
- It extracts a section of a string and returns it as a new string without modifying the original string.
- The method takes two parameters. string.slice(indexStart, indexEnd)
let str = "JavaScript";
console.log (str.slice(0)); //JavaScript
console.log (str.slice(1)); //avaScript
console.log (str.slice(0,1)); //J
console.log (str.slice(0,4)); //Java
console.log (str.slice(1,4)); //ava
console.log (str.slice(-3)); //ipt
console.log (str.slice(0,-6)); //Java
replace() :
- replace() - replaces only first match
- /text/g - replaces all matches using regex
- /text/i - 'i' makes it case-insensitive
- replaceAll() replaces all matches
let str = "Hello world world";
console.log(str.replace("world", "JS")); //Hello JS world
console.log(str.replace(/world/g, "JS")); //Hello JS JS
console.log(str.replace(/WORLD/g, "JS")); //Hello world world
console.log(str.replace(/WORLD/i, "JS")); //Hello JS world
console.log(str.replace(/WORLD/gi, "JS")); //Hello JS JS
console.log(str.replaceAll("world", "JS")); //Hello JS JS
split() :
- The split() method in JavaScript divides a String into an ordered list of substrings, puts these substrings into an array, and returns that array. It does not modify the original string.
let text = "Hello world from JavaScript";
const words = text.split(" ");
console.log(words);
let str = "Hello,world,from,JavaScript";
const arr = text.split(",");
console.log(words);
Output :
['Hello', 'world', 'from', 'JavaScript']
['Hello', 'world', 'from', 'JavaScript']
let str = "hello";
console.log(str.split("").reverse().join(""));
Output :
olleh
at()/charAt() :
let text = "JavaScript";
console.log(text.at(0)); //j
console.log(text.at(-1)); //t
console.log(text.at(10)); //undefined
console.log(text.charAt(0)); //J
console.log(text.charAt(-1)); //
console.log(text.charAt(10)); //
Top comments (0)