Hello Guys today i will discuss reverse() array method in javascript
reverse() method is used to reverse array elements
Example -
[1,2,3,4,5]
After reversing
[5,4,3,2,1]
Let's get started...
Code Example 1 -
const numberArray = [1,2,3,4,5];
const stringArray = ['A','B','C','D','E'];
const booleanArray = [true,true,false,true,false];
const objectArray = [
{
name:"Shubham",
age:21
},
{
name:"Shivam",
age:25
},
{
name:"Bharat",
age:22
},
]
const mixedArray = [1,2,3,"A","B","C",true,false,
{
name:"Shivam",
age:25
}
]
console.log(numberArray.reverse())
console.log(stringArray.reverse())
console.log(booleanArray.reverse())
console.log(objectArray.reverse())
console.log(mixedArray.reverse())
Output -
[ 5, 4, 3, 2, 1 ]
[ 'E', 'D', 'C', 'B', 'A' ]
[ false, true, false, true, true ]
[
{ name: 'Bharat', age: 22 },
{ name: 'Shivam', age: 25 },
{ name: 'Shubham', age: 21 }
]
[ { name: 'Shivam', age: 25 }, false, true, 'C', 'B', 'A', 3, 2, 1 ]
- As you can see we have reverses the arrays containing number, strings, booleans, objects and mixed.
Code Example 2 - How to reverse a string
const string = "hello"
const stringArray2 = []
for (let i of string){
stringArray2.push(i)
}
stringArray2.reverse()
let reversedStr = stringArray2.join("").toString()
console.log(newStr)
Output -
olleh
- As you can see first we have converted the string into array with each character as an element of array then we applied the reverse() method on this array.
- After that we have used the join("").toString() to again convert the array into string again.
THANK YOU FOR CHECKING THIS POST
^^You can help me by some donation at the link below Thank you๐๐ ^^
โ --> https://www.buymeacoffee.com/waaduheck <--
Also check these posts as well
https://dev.to/shubhamtiwari909/javascript-map-with-filter-2jgo
https://dev.to/shubhamtiwari909/e-quotes-3bng
https://dev.to/shubhamtiwari909/deploy-react-app-on-netlify-kl
Top comments (23)
Another one, this time with recursion:
๐
yeah but i only tried to discuss the inbuilt reverse() array method but this one liner implementation is also cool ๐
Say no more:
๐๐
I'm loving this posts you do on your learning, following up to read more ๐
Yeah this one is also good but then I had to explain this also and i am trying to show everything as a beginners perspective so later they can simplify the code on their own and come with these type of one liner solutions ๐
Also I want to explain join and split in a separate posts that's why I didn't explained much about them in this blog ๐๐
seen 1000 times and wrong 1000 times...
Yes yees we know, that doesn't mean it's wrong. You don't need to deal with emojist most of the time and when you do, simply
There are multiple ways to deal with that ๐คท๐ปโโ๏ธ
the only correct way: Intl.Segmenter!!
it's not about emojis, it's plain and simple also about characters in other languagesโฆ
dev.to/lukeshiru/comment/20d68
Intl.Segmenter is not working with Firefox
use a polyfilll
So I tried to break this down a bit
but this won't work for some ZWJ Sequences (multiple emojis joined with a zero-width joiner character being displayed as a "single emoji") and I was wondering why.
If we look at the Unicode Technical Standard, @lukeshiru you pick the emoji wisely to showcase.
Actually it's doing "great" reversing the whole thing as it did with
The issue I think is that the "computer" can't be joined with the "skin tone" and we see this weird symbol because skin tone does not work standalone.
Also I noticed that destructuring has issues with words in hindi as well:
Thus it actually works "fine", or as intended from the computation point of view, it's just that we don't have a valid "translation" to show the result, let's say
computer ‍ skin-tone-medium ‍ human
and something similar should be happening with hindi words (and presumably other languages).Whenever you need to work with ZWJ Sequences or some specific languages,
Intl.segmenter
is the option because it simply doesn't break those joined unicodes into something "unreadable". And to be precise it doesn't reverse the icons.If the standard covered symbols to be directional let's say "faceโairBlow"๐ฎโ๐จ and then "airBlowโface" being the opposite (blowing to the left) and also joining the modifiers to the compatible icons (either be right or left), every emoji sequence could be effectively reversed without breaking the "emoji" render.
On the other hand I can't understand -yet- why Firefox is not supporting it, I'll search to see if I can find the reason somewhere.
Do you guys have more detail on that?
Edit: I get some info about it here if you want to check. TL;DR It will be supported in short (theoretically).
this goes directly above my head ๐๐
One thing to note is that
reverse()
works in-place (aka destructively). This is important when you pass your array around to other functions or objects. For example:Yeah it changes the original array
Agree. That's one thing I love of coding. Each little bloody thing you can pick like that, as you go further in detail it has tones of nuances, quirks, things to take care when defining, various ways to reach something similar and so on ๐
Hey Joel i wanted to ask something that is i am a fresher in web development and student also and i am impressed by your knowledge like you know everything in detail
I want to know from where and how can I improve my knowledge and skills like you and can become a better web developer
Hey @shubhamtiwari909 actually you've people with tones of knowledge in the community and here in the post, like @lukeshiru who I found involved in many interesting comments and probably others which I may not recognize from other posts/discussions.
My workaround to learn more is to practice as much as I can and learn about stuff to bring new concepts to develop, new features to use and searching for different ways to reach the same to understand the differences so I can know when and where to use one or another.
I'm working as dev since just a decade ago so that's much less experience than many people here.
I learnt a lot in many projects having seasoned seniors next to me that were nice people and shared the knowledge and answering my child-ish questions (I'm always asking for the reason to be of the things and trying to understand the quirks and details) so I try to do the same in both my job and though this community.
In this specific case I knew almost nothing about this topic which is the emojis breaking when using
split
and I was itchy to know why does this happen so I began looking for information on the Internet, doing tests on my own, searching more with queries based on my test results... till I fulfilled my need for knowledge.I knew nothing because the lack of experience. Never had to deal with that, and here's where the community adds an important value to the learning process.
Now, my future "me" will know this details if I need to use them.
If this was something more complex to remember I may write a post on that so anytime I need to remember the details of some concept I can come back to my own post and get what I need.
On the other hand some books helped me to understand some things in the past but not everyone likes to read as much.
I'm here because I prefer to read tech stuff than watching someone on YouTube explaining it. It's faster, I can easily copy-paste concepts, keywords to search later on and take my notes easily.
Also I wrote this post which I want to re-write (a second version of it) with the support of a Psychologist in a future.
Hope it helps you in any way :)
Also check the profiles of all that nice people that came to comment and follow the ones with posts that interests you or that may be of interest for your future "you"! ๐
Best regards
Thank you for telling me all of this
I asked this question because I had an interview today and the questions were asked about CSS but the questions were very tricky like which tags have an inline display property by default in HTML, What happens when we try to give width and height to "span" tag , etc and i wasn't able to answer those correctly because I didn't know about the in depth details and also these type of questions never came into my mind while learning that's why I asked how to get deep and detailed knowledge about web development
Same happens with Javascript also but i am practicing it practically and with multiple cases so i am getting little bit in good that
Well, as I've previously said, the only way is to practice, reading, and writting posts about the topic are also beneficial
Complete CSS Guide for beginners and not so beginners
JoelBonetR ใป Jun 23 '20 ใป 30 min read
You force yourself into a bit of I+D+i, looking at the details a bit in deep, to ensure you don't publish wrong information.
Thank you for guidance ๐
Np. By the way, the question itself was a bit... uggly-ish if they really asked you to name all HTML tags that are
display: inline
by default.I can name
<span>
, maybe<i>
... but the trick on the question is that most default values are not in the CSS spec ๐ are just browser default styling and in certain cases can differ from one to the other.Edit: adding this list.
Yeah the questions were trickier and i get confused and then rejected even it was for the internship interview they believed i have the indepth knowledge and when I asked where can I learn all the things in detail then the interviewer said "you can learn these things in the industry while creating projects in a company under your senior guidance"
I was like yeah that's why I applied here to get that guidance and knowledge ๐๐
๐๐๐๐
For Indic text, I use this regular expression:
/\p{L}\p{M}?|\S|\s/gu
."เคนเคพเคฏ เคเคช เคเฅเคธเฅ เคนเฅ".match(/\p{L}\p{M}?|\S|\s/gu).reverse().join(""); // "เคนเฅ เคธเฅเคเฅ เคชเค เคฏเคนเคพ"
It won't work with Emoji sequences, and with glyphs resulting from a combination of more than two characters ("เคเฅเคฆเฅเคง" for example), but it's the closest thing to
Intl.Segmenter
that I was able to come up with.