Related Topics
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36

JAVASCRIPT
let str = "Hello, world!";
let reversedStr = str.split('').reverse().join('');
console.log(reversedStr); // Output: "!dlrow ,olleH"
In this example, the split('')
method splits the string str
into an array of characters, reverse()
reverses the order of the array elements, and join('')
joins the array elements back into a string.
2. Using a loop: You can use a loop to iterate through the characters of the string in reverse order and build a new reversed string.
let str = "Hello, world!";
let reversedStr = '';
for (let i = str.length - 1; i >= 0; i--) {
reversedStr += str[i];
}
console.log(reversedStr); // Output: "!dlrow ,olleH"
In this example, the loop starts from the last character of the string (str.length - 1
) and appends each character to the reversedStr
variable, effectively building the reversed string.
3. Using recursion: Recursion can also be used to reverse a string by recursively appending the last character to the reversed version of the remaining substring.
function reverseString(str) {
if (str === '') {
return '';
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}
let reversedStr = reverseString("Hello, world!");
console.log(reversedStr); // Output: "!dlrow ,olleH"
In this recursive example, the reverseString()
function checks if the input string str
is empty. If it is, an empty string is returned. Otherwise, the function calls itself with the remaining substring (str.substr(1)
) and appends the first character of the original string (str.charAt(0)
) to the reversed version of the remaining substring.
String reversal can be useful in various scenarios, such as data processing, text manipulation, or implementing algorithms. The specific approach to use depends on the requirements and constraints of your application.
let str = "Hello";
let paddedStr = str.padStart(10, "*");
console.log(paddedStr); // Output: "*****Hello"
In this example, str.padStart(10, "*")
pads the string str
with asterisks (*
) at the beginning until it reaches a length of 10 characters. The resulting string is stored in paddedStr
.
2. padEnd()
method: The padEnd()
method pads the end of a string with a specified character(s) until it reaches the desired length.
let str = "Hello";
let paddedStr = str.padEnd(10, "*");
console.log(paddedStr); // Output: "Hello*****"
In this example, str.padEnd(10, "*")
pads the string str
with asterisks (*
) at the end until it reaches a length of 10 characters. The resulting string is stored in paddedStr
.
It’s important to note that if the desired length is already less than or equal to the original string’s length, no padding is applied, and the original string is returned as-is.
Both padStart()
and padEnd()
methods take two parameters: the desired length and the padding character(s). The length parameter specifies the minimum length of the resulting string, and the character(s) parameter represents the padding character(s) to be added. The padding character can be any string or a single character.
The string padding methods are useful when you need to align strings, format output, or ensure consistent lengths for display purposes. By utilizing padStart()
and padEnd()
, you can easily add padding characters to strings in JavaScript.
let str = " Hello, world! ";
let trimmedStr = str.trim();
console.log(trimmedStr); // Output: "Hello, world!"
In this example, str.trim()
removes the leading and trailing whitespace from the str
string, resulting in the trimmed string "Hello, world!"
.
It’s important to note that trim()
only removes whitespace at the beginning and end of the string and not within the string itself.
2. Using regular expressions: Regular expressions can be used to remove all occurrences of whitespace within a string. The replace()
method, combined with a regular expression, can accomplish this.
let str = "Hello, world!";
let noWhitespaceStr = str.replace(/\s/g, "");
console.log(noWhitespaceStr); // Output: "Hello,world!"
In this example, the regular expression /\s/g
matches all whitespace characters (\s
), and the replace()
method with an empty string ""
as the replacement removes all whitespace occurrences.
The regular expression /\s/g
uses the \s
pattern to match whitespace characters, and the g
flag performs a global search to replace all occurrences.
3. Using split()
and join()
: You can split the string into an array of substrings using split()
, specifying the whitespace as the delimiter, and then join the array back into a string using join()
.
let str = "Hello, world!";
let noWhitespaceStr = str.split(" ").join("");
console.log(noWhitespaceStr); // Output: "Hello,world!"
In this example, str.split(" ")
splits the str
string into an array of substrings at each whitespace occurrence, and join("")
joins the array elements back into a string with no whitespace.
These methods provide different ways to remove whitespace from a string in JavaScript. Choose the method that best suits your specific use case and the whitespace removal requirements of your code.




Popular Category
Topics for You
Introduction
Html page 1
Html page 2
Html page3
Html page4
HTML Elements and structure
Html page 5
Html page 6
Html page 7
HTML Headings and Paragraphs
Html page 8
Html page 9
Html page 10
HTML Lists and Tables
Html page 11
Html page 12
Html page 13
HTML Forms and Input Fields
Html page 14
Html page 15
Html page 16
HTML Images and Media
Html page 17
Html page 18
HTML Links and Anchors
Html page 19
Html page 20
Html page 21
HTML Styles and Formatting
Html page 22
HTML Semantic Elements
Html page 23
Html page 24
HTML Attributes
Html page 25
Html page 26
HTML JavaScript Integration
Html page 27
Html page 28
Html page 29
Html page 30
HTML Document and Browser Support
Html page 31
Html page 32
HTML5 New Elements and Attributes
Html page 33
Html page 34
Html page 35
Html page 36
HTML Accessibility and Web Standards
Html page 37
Html page 38
Html page 39
HTML Responsive Design and Mobile Devices.
Html page 40
Html page 41
Html page 42
Introduction
Data Structure Page 1
Data Structure Page 2
Data Structure Page 3
Data Structure Page 4
Data Structure Page 5
Data Structure Page 6
Data Structure Page 7
Data Structure Page 8
String
Data Structure Page 9
Data Structure Page 10
Data Structure Page 11
Data Structure Page 12
Data Structure Page 13
Array
Data Structure Page 14
Data Structure Page 15
Data Structure Page 16
Data Structure Page 17
Data Structure Page 18
Linked List
Data Structure Page 19
Data Structure Page 20
Stack
Data Structure Page 21
Data Structure Page 22
Queue
Data Structure Page 23
Data Structure Page 24
Tree
Data Structure Page 25
Data Structure Page 26
Binary Tree
Data Structure Page 27
Data Structure Page 28
Heap
Data Structure Page 29
Data Structure Page 30
Graph
Data Structure Page 31
Data Structure Page 32
Searching Sorting
Data Structure Page 33
Hashing Collision
Data Structure Page 35
Data Structure Page 36
Go through our study material. Your Job is awaiting.