Cool Things about JavaScript

JavaScript is cool. I don’t know why, but that’s what I hear. So I decided to dedicate a quiet week to learning all about JavaScript from this book Eloquent JavaScript. The author is great and the explanations/examples are clear and interesting.

I’m almost halfway through the book and these are just a few of the cool things I’ve learned:

Lexical Scoping

Lexical Scoping is not just fun to think about it can be very powerful. What is going to happen when the scope of a function is determined by the function that calls it?

You can get functions to do all sorts of crazy stuff this way.

Here are a couple different ways to get it into your head:

From Eloquent JavaScript: 
“…each local scope can also see all the local scopes that contain it. The set of variables visible inside a function is determined by the place of that funciton in the program text. All variables from blocks around a functions definition are visible — meaning both those in function bodies that enclose it and those at the top level of the program. This approach to variable visibility is called lexical scoping.” – Marijn Haverbeke

Helpful StackOverflow

Helpful Blog

“The scope of an inner function contains the scope of a parent function.”

My example:
Each local scope, can see the variables of the containing scope. Each new set of braces does NOT create a brand new environment. It’s like the octopus holds another octopus.

var octopus1 = function(){
  var smallArms = 5;
  var octopus2 = function(){
    var largeArms = 8;
    console.log(largeArms > smallArms);

--> True

It’s like each new function is a new Octopus, and one of the octopus tentacles is on the octopus that called it.

It seems like kind of a backward metaphor. Like the thing that harkens for information ends up being help by the provider.

So the biggest octopus will be found at the most inner scope. Holding a chain of sequentially smaller octopuses.

Other things that I found cool…

NaN and Undefined (Null?)

Very much a pitfall to be aware of, on second thought this isn’t really cool, but just good to know.

Here’s a helpful blog that helped me wrap my brain around the reasoning and differences

He references this source for his blog: which I’ve found helpful .

NaN: Not a number, bad arithmetic

Undefined: variable doesn’t have a value

Null: Not a value, will return empty object.

Infinity and -Infinity

As my little cousin would say, “You’re it times infinity!”

For loops like C++

for(var i == 0; i < 10; i++){
  console.log("ahh yeah");

C++ was my first language so these for loops remind me of my first CS class, even though that’s where I got my first C on an exam, it was the first time I felt the power of computing.

Strings can be wrapped ‘with single quotes’ or “double quotes”

‘That\’s nice’
“How convenient!”

Ternary Operator

console.log(true ? 1 : 2)
// --> 1

console.log(false ? 1 : 2)
// --> 2

This is cool. I’ve never used a ternary operator. I could see this saving lots of lines for if statements.

Variables as Tentacles

‘rather than boxes. They do not contain values; they only grasp them … when you need to remember something, you grow a tentacle to hold on to it or you reattach one of your existing tentacles to it.’ – Marijn Haverbeke

Like lexical scoping, this is a cool metaphor for looking at your programs and functions. Gosh octopi are cool and smart!

do loops

Do this…
Then loop if the condition is met.
When you just need to get something done. DO it!


Optional Arguments are handled nicely.

function power(base, exponent){
  if (exponent == undefined)
    exponent = 2;
  var result = 1;
  for(var count = 0; count < exponent; count++)
    result *= base;
  return result;

// --> 16
 console.log(power(4, 3));
// --> 64

Ahh that’s sooo sweet.

Looking forward to continued JavaScript development and I hope this list has whet your appetite for more JavaScript to come.

Happy Friday,

If that doesn’t load, here another photo that I enjoy:

Pickup truck full of kegs
This pickup truck looks like Seattle

Leave a Reply

Your email address will not be published. Required fields are marked *