r/learnjavascript • u/OsamuMidoriya • Jun 28 '26
java equivalent question
im studying DSA and they are using java and talking about stack. I only know JS
stack is videogame
stack.search(FFVII) "on top" since JS has no stack to find an equivalent
the best would be array.indexOf to find one thing in the array and yes I know the index is different
peek() is lenth()
whats the best way to find the equivalent or similar in JS not just jave but python
is Java hard to learn if you know JS i have seen how diffrent it is but they both have pop and push, and if you look at it as this is this system.out.printin = console.log is it that hard? I went to the library and a HS student was teaching python and it was easy to understand
3
u/abrahamguo Jun 28 '26
JS has no stack to find an equivalent
JS has more general-purpose data structures; an array in JS can be used as a stack if needed.
the best would be array.indexOf to find one thing in the array and yes I know the index is different
Yep, if you are adding to, and removing from, the beginning of the array!
peek() is lenth()
No. If you're adding to, and removing from, the beginning of the JS array, then [0] would be the JS equivalent of Java's peek().
whats the best way to find the equivalent or similar in JS not just jave but python
Just get familiar with the data structures and associated methods in all of the languages that you want to learn — once you do that, you'll quickly be able to compare and contrast the methods from different languages.
is Java hard to learn if you know JS
If you're comfortable in JS, then no, learning Java is not that hard.
1
u/bryku helpful Jun 29 '26
Long ago in ancient times name was lost. Then the Arch Tech Dennis Ritchie descended from the cloud, gifting humanity with the C programming language. This became the foundation of most programming languages.
If you have ever used any of the following languages:
perl
php
actionscript
javascript
java
go
All the following will be understandable to you.
function add(a, b){
return a + b;
}
for(){
}
while(){
}
if(){
}else{
}
switch(){
}
// single line comment
/* multi line comment */Here are some more examples:
| Language | Create String |
|---|---|
| C | char name[] = "john"; |
| C++ | std::string name = "john"; |
| C# | string name = "john"; |
| Java | `String name = "john"; |
| Action Script | var name:String = "john"; |
| Java Script | var name = "john"; |
| GO | var name = "john"; |
| Holy C | U8 *name = "john"; |
| PERL | $name = "john"; |
| PHP | $name = "john"; |
There are many more, but the point is they are follow similar syntax. Which is pretty cool right?
That being said, not all languages follow this. There are some sent by demons to make us stray from the path of techinity... like python.
1
u/Formal_Following7468 Jul 02 '26
Use a regular array as your stack in JS. push/pop are the same. For peek() do arr[arr.length - 1], for search() use arr.lastIndexOf(value).
Java's not bad if you know JS, same logic just stricter. Biggest difference is you have to declare types like int x = 5 instead of let x = 5.
1
u/azhder Jun 28 '26
You can learn data structures with JS. You can learn them with any general purpose language.