Skip to main content

JavaScript Rocks

Here's a bold statement... (so I made the font bold)
JavaScript is the best programming language


Here's why...

  1. The language is extremely powerful.  Let's imagine that we are inventing a new programming language.  The most important thing we will focus on is "power."  That means... write more elegant, more flexible, more readable programs... and write them even faster.
    1. Closures.  A completely awesome, overlooked feature of JavaScript that we all (hopefully) take for granted.  Closures allow you to create a new instance of a function which retains access to the context in which it was created.  Examples are here: http://en.wikipedia.org/wiki/Closure_(computer_science).
    2. Dynamic data types.  JavaScript variables aren't restricted to any particular datatype.  Store a String, store a number, or store an Object.  JavaScript automatically casts datatypes back and forth as needed.
    3. Objects are basically associative arrays, and vice-versa.  Everyone loves associative arrays.  The end.
    4. First-class and higher-order functions.  Variables can store functions.  You can pass functions as arguments to another function.  A function can return another function.  Functions in JavaScript can have an unlimited number of arguments.  Anonymous functions?  No problem.  Functions in JavaScript are just awesome!
    5. OOP is easy.  Objects are made up of properties.  There is no distinction between properties and methods because functions are handled the same way as other datatypes.  JavaScript supports inheritance.  Constructors are just functions that are invoked using the new operator.
    6. JavaScript is prototype-based.  Object oriented code is awesome (reduce coupling, encourage encapsulation, code re-use, etc).  But, classes aren't the answer.  Classes are too strict, resulting in strictly defined Objects, each with fixed properties and methods.  With prototype-based OOP, you design Object prototypes (i.e classes) that can evolve throughout the lifetime of the program.  Adding a new method to all instances of an Object can be done very efficiently at run-time with one line of code.
    7. JSON is a subset of the object literal notation of JavaScript.  Use eval() on a JSON string and you successfully implemented Object deserialization - although in practice you would actually use JSON.parse().  Still... awesome!
    8. No concurrency headaches!  That's right -- JavaScript is single-threaded.  There is an event loop (hidden from view) and there are events and event handlers.  Start an I/O operation with function call.  When the I/O operation completes, an event handler function is called.  Piece of cake!  You don't need concurrency -- a single-core CPU only has a single thread anyway.  If you want to utilize multiple CPU cores or build a computer cluster with Node.JS, just run multiple instances of your application and allow them to communicate via Sockets.  Threads are slow and waste a lot of memory anyway.
    9. Other dynamic programming features.  Eval(), func.call(), func.apply() are all features that should be used sparingly, yet they are still available when their use is appropriate.
      http://www.w3schools.com/jsref/jsref_eval.asp
      http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
  2. JavaScript is widely adopted.  Almost every operating system has a web browser with JavaScript support.  PCs, Macs, Cell phones, handhelds, tablets, & netbooks all have browsers that support JavaScript.  While it is mostly used as a language for client-side browser applications, it is being increasingly used as a general programming language (even compiled)... Node.js is a good example.  Also, due to JavaScript's wide adoption, JavaScript has a very large development community.  There are a ton of JavaScript tutorials all over the Internet.
  3. A simple library backed by a ton of 3rd party libraries.  JavaScript on the browser has an extremely simple API.  The DOM is relatively straight-forward, as well.  jQuery is just one example of a library that makes JavaScript much easier to use and much more readable.  Node.js is also relatively simple.
  4. JavaScript is fast.  JavaScript is among the fastest interpreted languages thanks to many recent advancements.  (thank you, Google Chrome, et. al.).  On some platforms, you can compile JavaScript.  From what I've read, it's pretty fast, too.
  5. I forgot to mention the obvious.  Developing in JavaScript is free.
JavaScript will soon become a common server-side programming language (perhaps running like CGI or NodeJS will take over).  Web servers will be used for serving static pages.  Technologies like JavaScript client-side and NodeJS will run the bulk of a "Web 3.0" application.  Cutting-edge database back-ends like Redis will lead developers to lightning fast real-time applications.

If you don't know JavaScript learn it.  If you hate JavaScript (which I did for many years), look again.

JavaScript ROCKS!  The end!

Comments

Popular posts from this blog

Developing a lightweight WebSocket library

Late in 2016, I began development on a lightweight, isomorphic WebSocket library for Node.js called ws-wrapper .  Today, this library is stable and has been successfully used in many production apps. Why?  What about socket.io ?  In my opinion, socket.io and its dependencies are way too heavy .  Now that the year is 2018, this couldn't be more true.  Modern browsers have native WebSocket support meaning that all of the transports built into the socket.io project are just dead weight.  On the other hand, ws-wrapper and its dependencies weigh about 3 KB when minified and gzipped.  Similarly, ws-wrapper consists of about 500 lines of code; whereas, socket.io consists of thousands of lines of code.  As Dijkstra once famously said: "Simplicity is prerequisite for reliability." ws-wrapper also provides a few more features out of the box.  The API exposes a two-way, Promise-based request/response interface.  That is, clients can request data from servers just as easily as se

Computer Clocks Cause More Issues

Two nights ago, a leap second was added to system clocks running Linux, causing much-undesired havoc. On July 1st at 12:00 AM UTC, both of my Amazon EC2 instances fired an alarm indicating high CPU usage. I investigated to find that it was MySQL that was eating all of the CPU. I logged in and ran SHOW PROCESSLIST to find that no queries were running (these servers don't get hit much after business hours). I stopped MySQL, CPU utilization dropped back down to 1-3% (as normal). I restarted MySQL, and it started eating a lot of CPU again. Then, I restarted the server (shutdown -r now), and the problem went away. Both servers had the exact same problem (running Ubuntu 12.04 LTS). In my particular case, MySQL began eating CPU, even after being restarted.  It was a livelock. The only relevant item I saw in the syslog was: Jun 30 23:59:59 hostname kernel: [14152976.187987] Clock: inserting leap second 23:59:60 UTC Oh yeah... leap seconds.  Those are super important.

JavaScript Sticky Footer and Scroll Effect

This post talks about two different HTML/JavaScript effects: How to keep a page footer stuck at the bottom of the browser window. How to create a scrolling <div> without using a scroll bar OK. So... you have a website. You want a header stuck at the top of your page and the footer stuck at the bottom of your page. The stuff in the middle, you want to be able to scrollable. But, you don't want those ugly scrollbars to the right of your scrollable text. Maybe, instead, you'll have up arrows and down arrows above and below your <div>. When you mouseover the arrows, the text in the <div> will move up or down and create a scrolling effect. Suppose your page looks like this... <html> <head> <title>Test</title> </head> <body> <div style="position: relative; width: 700px; margin-left: auto; margin-right: auto;"> <div id="header">Header</div> <div id="scrollUp&q