A String is a sequence of characters used to store text data.
let name = "Rohan";
let city = "Bangalore";
Strings can be enclosed in "", '' or ``
👶 Creating strings
For creating a string, new String("Hello") is rarely used, primitive and simple way is used to define and declare strings.
let message = "Hi";
🔒 String Immutability
Strings are immutable, meaning their characters cannot be changed directly after creation.
let str = "Hello";
str[0] = "Y";
console.log(str); // "Hello"
String methods return new strings instead of modifying the original string.
📞 Accessing Characters
Characters can be accessed using square bracket notation or the at() method. Indexing starts from 0.
let str = "Hello";
console.log(str[0]); // "H"
console.log(str.at(0)); // "H"
console.log(str.at(-1)); // "o" (last character)
Note
Negative indices work with at(), but not with square bracket notation.
String properties
length property
let str = "javascript is amazing";
console.log(str.length); // returns the length of the string including blank spaces.
⚙️ String methods
- ToUppercase() - to convert all characters into uppercase alphabets
console.log("hi".toUpperCase()); // "HI"
- toLowerCase() - to convert all characters into lowercase alphabets
console.log("HI".toLowerCase()); // "hi"
- trim methods
console.log(" hi ".trim()); // "hi"
console.log(" hi ".trimStart()); // "hi "
console.log(" hi ".trimEnd()); // " hi"
- includes() - returns true if the input string is a substring of the string
console.log("Javascript".includes("Java")); // true
console.log("Javascript".includes("Python")); // fasle
- startsWitn and endsWith methods
console.log("Javascript".startsWith("Java")); // true
console.log("Javascript".startsWith("Python")); // fasle
console.log("Javascript".endsWith("script")); // true
console.log("Javascript".endsWith("lang")); // fasle
- indexOf() and lastIndexOf() - return the first or last index of the substringin the string, or else reutrn -1 if not present
console.log("java".indexOf("a")); // 1
console.log("java".lastIndexof("a")); // 3
- slice() - used to extract part of the string
console.log("Javascript".slice(4)); // from the 4th till last index --> "script"
console.log("Javascript".slice(0,4)); // from 0th to 4 -1 = 3rd index --> "Java"
console.log("Javascript".slice(-6)); // last 6 characters --> "script"
substring()- extracts a part of a string using start and end indices. The end index is excluded.
console.log("Javascript".substring(0, 4)); // "Java"
console.log("Javascript".substring(4)); // "script"
Note
Unlike slice(), substring() treats negative indices as 0 and swaps the arguments if the start index is greater than the end index.
- replace() and replaceAll() - used to substitute parts of the string with replacement
console.log("ha ha ha".replace("ha", "hi")); // "hi ha ha"
console.log("ha ha ha".replaceAll("ha", "hi")); // "hi hi hi"
- split(
delimiter) - used to convert string into and array
console.log("apples,mangoes,bananas".split(",")); // ["apples","mangoes","bananas"]
- concat() - to attach a string in the end
console.log("Merry".concat(" and ", "James")); // "Merry and James"
- repeat() - used to repeat a string for a number of times
console.log("*".repeat(3)); // "***"
- charAt() - returns the character at the index
console.log("Javascript".charAt(2)); // "v"
- charCodeAt() - returns the ASCII value of the characte at the index
console.log("A".charCodeAt(0)); // 65
- padStart() and padEnd() - pad the string to the spefic length by contcatinating the given character to the start or the end
console.log("5".padStart(3,"0")); // "005"
console.log("5".padEnd(3,"0")); // "500"
- match() - to find substrings using regular expression
console.log("abc123".match(/\d+/)); // "123"
- search() - finds the index of the substring
console.log("hello123".search(/\d/)); // 5
Note
String("Hello")returns a primitive string, whereasnew String("Hello")creates a String object. Prefer primitive strings.console.log(typeof String("Hello")); // "string" console.log(typeof new String("Hello")); // "object"
Comments
Join the discussionNo comments yet. Be the first to comment.
Decode Javascript
Explore JavaScript one concept at a time, from the fundamentals to advanced topics. Learn through clear explanations, practical examples, and code as we uncover how JavaScript works and use it to build for the web.
