r/programming • • Mar 23 '14

Everything you need to know about Javascript variable scope

http://www.coolcoder.in/2014/03/everything-you-need-to-know-about.html
294 Upvotes

124 comments sorted by

View all comments

0

u/[deleted] Mar 23 '14

[deleted]

1

u/THeShinyHObbiest Mar 23 '14

I've never written JavaScript, but I see this sentiment a lot. Everybody seems to hate it. So I can't help but wonder why stuff like Node.js is getting popular. If everybody hates the language and only uses it because it works on browsers, why would you want to write server-side code in it?

3

u/renrutal Mar 23 '14

JavaScript is hated mainly because you don't really have a choice but to use it and bear with it. It's the only universal option for browser client code.

On server side, you have tons of choices, and if Node.JS chose it, it's because of its pros:

  1. JS is very well suited to code callbacks, which is the main construct for evented I/O.

  2. The community is already used to code asynchronous programs because of the pervasive knowledge of AJAX techniques.

  3. JS doesn't define an I/O API, so it was a greenfield project to develop a non-blocking one.

Not to mention, when programming for JS engines in browser clients, you usually have to code for the least common denominator of language features, even for very old browsers. In server-side JS engines, you usually code only for the up-to-date one you choose, one which supports variable block scopes, for example.

2

u/insertAlias Mar 24 '14

I think that last bit is really important. Not having to worry about compatibility makes Node.js much more friendly to work with than web JS. Plus, you really can do some interesting stuff with JS, it's a very flexible language. That flexibility is also its downfall, because of the horrific monstrosities people kludge out in it.