Posts

Showing posts with the label JavaScript String

Method chaining in string

What is Method chaining in string? Method chaining is a technique in JavaScript where multiple methods are chained together in a single statement to perform consecutive operations on a string. This allows for cleaner and more concise code by avoiding the need for intermediate variables or repeated function calls. For example, instead of writing separate lines of code to perform different string operations, you can chain the methods together to achieve the same result in a single line. Example 1: let myString = "Hello, World!" ;   // Instead of let uppercaseString = myString. toUpperCase ( ) ; let reversedString = uppercaseString. split ( "" ) . reverse ( ) . join ( "" ) ; console. log ( reversedString ) ; // Output: "!DLROW ,OLLEH"   // Method chaining let newString = myString. toUpperCase ( ) . split ( "" ) . reverse ( ) . join ( "" ) ; console. log ( newString ) ; // Output: "!DLROW ,OLLEH" In the abov...

Strings are Immutable in JS

The immutability of strings in JavaScript In JavaScript, strings are immutable, which means that once they are created, they cannot be changed. This means that any operation on a string will always return a new string rather than modifying the original string itself. Example 1: let str = "Hello" ; let newStr = str. concat ( " World" ) ; console. log ( str ) ; // Output: "Hello" console. log ( newStr ) ; // Output: "Hello World" In the example above, the `concat()` method is used to concatenate the string " World" to the original string "Hello". However, the `concat()` method does not modify the original string, it returns a new string that contains the concatenated values. Points to note: 1. Strings are immutable in JavaScript, meaning they cannot be changed after they are created. 2. Operations on strings always create new strings. 3. Methods like concat(), slice(), trim(), etc., that appear to modify strings actu...

String methods

String Methods JavaScript provides several built-in methods for manipulating strings. These methods allow you to perform various operations on strings, such as extracting substrings, searching for specific characters or patterns, converting case, and more. In this guide, we will explore some of the most commonly used string methods in JavaScript, along with their syntax and examples. 1. length Syntax: string.length Description: 'Length' is not a method but a property in JavaScript. It allows you to retrieve the length of a string, i.e., the number of characters it contains. It is a read-only property, so you cannot modify it. Example: const text = "Hello, World!" ; const length = text. length ;   console. log ( length ) ; // Output: 13 2. charAt(index) Syntax: string.charAt(index) Description: Returns the character at the specified index in the string. Example: const str = "Hello, World!" ; console. log ( str. charAt ( 0 ) ) ; // Output: "H...

Escape character

What is an escape character in JavaScript? In JavaScript, an escape character is used to represent characters that are not possible to include directly in a string or have a special meaning within a string. An escape character is denoted by a backslash (`\`) followed by a specific character code or sequence. The escape character allows us to include special characters, such as quotes, newlines, and tabs, within a string. Here's an example that demonstrates the use of an escape character to include a double quote within a string: let message = "She said, \" Hello! \" " ; console. log ( message ) ; // Output: She said, "Hello!" In the example above, the backslash before the double quote (`\"`) is used as an escape character to include the double quote within the string. Without the escape character, the string would be terminated at the first double quote. Escape character codes In JavaScript, various escape character codes are available to repre...

Template literals

What are JS Template literals? JS Template literals, introduced in ES6, are a type of string that allows for easier string interpolation and multiline strings. They are enclosed by backticks (`` ` ``) instead of single or double quotes used for regular strings. // Example of a template literal const name = "John" ; const age = 25 ; const message = `My name is $ { name } and I am $ { age } years old.` ; console. log ( message ) ; // Output: My name is John and I am 25 years old. Template literals provide a more concise way to concatenate strings and embed expressions within them. They make it easier to create dynamic strings with variables and evaluate expressions. String interpolation String interpolation is the process of including expressions within a string. With template literals, expressions can be embedded using `${}`. When the template literal is evaluated, the expressions are replaced with their corresp...