Mobile

How to reverse a String in JavaScript Tutorial



In this tutorial, you’ll learn how to reverse a string in JavaScript including a way to do it with using a for loop which is helpful if you get asked to solve this challenge at interview. Get my free 32 page eBook of JavaScript HowTos 👉

Other videos in the JavaScript Snippets series:

How to find the longest word in a String with JavaScript:
Find the largest number in an array JavaScript Tutorial:
Check if a String is a palindrome with JavaScript Tutorial:
Javascript String Length: How to determine the size of a string:
JavaScript join method: How to merge arrays into one value:
JavaScript How To Remove An Item From Array Tutorial:
Javascript How To Convert String To Number Tutorial:
JavaScript Create HTML Element: How to dynamically add tags to your pages:
JavaScript String Contains: How to check a string exists in another:
Link JavaScript to HTML: How to run your JavaScript code in the browser:
JavaScript Copy Array: How to make an exact copy of an array in JavaScript:
JavaScript Capitalize First Letter: How to make strings and arrays sentence case:
Javascript Print To Console Tutorial: Different ways to output data to the console:
Don’t forget to subscribe to the Junior Developer Central channel for more videos and tutorials!

Whilst it’s not the most common of tasks to do as a Junior Developer, finding out how to reverse a string in JavaScript is handy to know as it introduces you to a few new concepts in terms of handling arrays.

But wait!? A string isn’t an array!

You’re right it’s not but unfortunately for us there is no in-built function in JavaScript which will help you reverse a string in JavaScript. For example, you can’t just call str.reverse() to get a reversed version of the string.

However, as you’ll discover in the video, arrays in JavaScript do have a reverse function available to them. So if we could convert a string in to an array (with each item in the array being a character from the string) we could then reverse it easily with the Array.prototype.reverse() function.

In the tutorial you’ll learn how to do exactly this by using the split() function on the original string and then using the join() function to re-assemble it once it’s been reversed.

You’ll see that this process is very simply however sometimes you’ll be asked (perhaps at a job interview) how to reverse a string in JavaScript without using reverse method. In this instance, the interviewers are checking you can handle basic algorithmic challenges without relying on inbuilt functions.

So, in order to learn how to reverse a string in JavaScript without using reverse method, in the video we’ll discuss how to reverse a string in JavaScript with for loops. Whilst this isn’t the most elegant of ways to do it, it is fairly straightforward and it’s always good to know different ways of accomplishing the same task.

how to reverse a string in javascript with for loop, how to reverse words in a string in javascript, how to reverse a string in javascript w3schools, how to reverse a string in javascript without using reverse method, how to find reverse of a string in javascript, reverse a string, reverse string, learn javascript, javascript snippets, how to reverse a string in javascript, javascript, javascript tutorial, programming, learn javascript programming

Xem Thêm Bài Viết Về Mobile Khác: https://sealsystem.org/mobile

Nguồn: https://sealsystem.org/

11 Comments

  1. Hey dude, loved the vid, I was struggling for a little while to find the proper syntax and structure to do this properly with the loop.

    Turned out I was doing text.length and not including length – 1 like you did in the video – I can assume this is why it returned undefined then my string value.

    Just quickly if you don't mind answering, I still can't picture why this is the case though?

    In my mind, you should start at the end length, to get the last character? (There must be something I'm missing as it only works with length – 1…)

    Say you're working with: const text = "string";

    The text.length would be 6. So to reverse all six letters, surely you would need to start at text.length so it starts at 6, not -1 so it starts at 5.

    I hope that makes sense, it's just something I'm struggling to process!

    Reply
  2. I love your style. But my question is how can you reverse only words with 6 or more letters in a given string. That is ''My second programs code'' turns to "My dnoces smargorp". Thank you

    Reply

Post Comment