🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this article, we will learn how to reverse a String in JavaScipt with an example.
Basically, we are going to learn different ways to reverse a String in JavaScipt.
Reverse String in JavaScript Example
Here are the three ways to reverse a String in JavaScript:
- With built-in functions
- With a for loop
- With recursion
main.js
Let's create a main.js file and add the following code to it:
// 1. With built-in functions
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString("Source code examples"));
// 2. With a for loop
function reverseStringUsingLoop(str) {
var newString = '';
for (var i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
// 3. With recursion
function reverseStringusingRecursion(str) {
return (str === '') ? '' : reverseString(str.substr(1)) + str.charAt(0);
}Note that the comments in the above code are self-descriptive.
index.html
To test JavaScript code, let's create an HTML file named index.html and add the following code to it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>Note that we have included the main.js file as an external JavaScript file:
<script src="main.js"></script>Run index.html in Browser
Run above index.html file in browser and open dev tools -> console to see the output of the JavaScript code:
selpmaxe edoc ecruoS
selpmaxe edoc ecruoS
selpmaxe edoc ecruoS
Related JavaScript Examples
- How to Empty an Array in JavaScript //Popular
- JavaScript Utility Methods //Popular
- Best JavaScript Utility Libraries //Popular
- Check if Map is Null or Empty in Java //Popular
- JavaScript LocalStorage Methods - setItem(), getItem(), removeItem() clear() and key() Example //Popular
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment