7

In my day-to-day work i mostly use C# and only use javascript occasionally, so please, javascript Gurus don't judge my questions roughly!

  1. Array implements the Stack by providing the push and pop method, but peek is missing, why? (yes it is trivial to implement, but still)
  2. Array implements Queue but the operations are named push-shift orunshift-pop instead of enqueue and dequeue, why name them differently? Is this inspired by Python and Ruby?
  3. Why APIs for Array, Stack and Queue are merged into one object, instead of segregating the interface and having different objects for that? Is it because the implementation is cheap?
  4. Semantically in many languages (C#, C++, Java) an Array is a continuous block in memory and is not re-sizable. On the other hand, the basic collection that allows easy addition of elements is a List (ArrayList or LinkedList or the like). Would it not be better if Array was named a List in javascript?
  5. How is Array implemented under the hood? Where can I find a very detailed description?
11
  • 1
    @NinaScholz, peek returns the last element, the same as pop but does not remove it from the stack. let stack = []; stack.peek = function () { return stack[stack.length - 1]; }; Commented May 6, 2017 at 20:13
  • 1
    While I don't have the answer to all of those questions, I can say that JavaScript in general tends to focus on flexibility more than anything else. Any object can be anything you want it to be depending on what methods it has. So it just seems natural for this kind of language that rather than providing multiple constructors, the same object can act like an array, a list, a queue or a stack (or any combination of them), depending on how you're using it and what you need it for. Commented May 6, 2017 at 20:17
  • 1
    In javascript, it's all objects, an array is basically just an object with length. Also, it's two different languages, with two very different use cases, why would everything be the same? Commented May 6, 2017 at 20:25
  • 2
    By the way, shift and unshift in JS aren't exactly the same as enqueue and dequeue in C#. shift and unshift both operate on the 0th index of the array, while enqueue adds to the end of the Queue and dequeue removes at the front. To use a JS array like a queue, you'd either use unshift and pop OR push and shift. (I hope I didn't get anything mixed up there.) Commented May 6, 2017 at 20:31
  • 2
    JS arrays are nothing more than reference type mutable objects and that's what you have. In other languages you have lists, sequences, vectors, stacks you name it. I guess in a loosely typed language like JS the array structure is chosen to provide an answer to all such needs in a simplistic manner. JS arrays let you implement an efficient solution to whatever you need within the boundaries of JS type system. Commented May 6, 2017 at 21:15

1 Answer 1

1
  1. JavaScript was invented in 10 days, peek was never added since, mainly because it is easy to implement, maybe one day.

  2. Differently from what you know yes

  3. It is a high level programming language, also check out the Typed arrays

  4. It could have been named otherwise, it is too late now.

  5. It depends on the Engine, mostly like in Python

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

Sign up to request clarification or add additional context in comments.

9 Comments

3. more importantly: its a parsed language.
@Jonasw: as opposed to…?
@Ryan theres no need to optimize datastructures, as performance is generally not in focus...
@Jonasw: What do you mean by “it’s a parsed language”? Most languages get parsed.
@Jonasw: Modern JavaScript engines are optimizing and have JIT compilers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.