Reverse a string in javascript using for loop
hi everyone in this video i'm going to discuss about how we can reverse a string in javascript using for loop so let me just get started with the coding part right away so i'm going to declare a string and the value of the string is let's say sunday and as usual i'm going to create a function with name reverse string it's a good practice to create functions so that we don't have to write the same logic again and again whenever we need it so i'm going to declare uh another variable over here which will contain my reverse string so i have declared this with an empty string so that it does not contain any undefined values then i'm going to start my for loop i'm going to start my iteration from the last element in that string so if i talk about the string with the value of sunday last element is y which is on position number five and the total length of the string is six so that means i have to start with a position five and that is also string length minus one so i'm going to do the same over here string length minus one position is greater than equal to zero position minus minus since i have to go down from five to zero and then i'm going to sum up everything in this drawing and in the end i'm just going to return the string over here and on top i'm simply just going to call this function so when i call this function and i pass the string i get the reverse string so it looks like we have a problem so let's take a look at the code again so i think i missed something oh god this is it so the spelling of length of song if i execute it again i get the string and let us test this one with the sentence as well and it goes well so this is the reverse sentence so this particular method is considered as one of the better methods when we talk about reversing the string as compared to the one with the reverse function but this is just not the best method to reverse the string as when we talk about the execution status in the back end so in my next video i will be discussing about another method where we are reversing a string but the execution is fast as compared to when we use a for loop or when we use a reverse function so thank you for watching and stay safe and stay healthy bye
https://www.youtube.com/watch?v=ACQD5oJCrOU