Value vs Reference Assignment in JavaScript

In my opinion, value vs reference assignment is an important topic that many JS beginners don’t stop and think about enough.

Let’s test your knowledge of this concept with some puzzles.

Puzzle #1:

This first puzzle is part of Douglas Crockford’s function challenges. You can watch some of his exercises in the free video sample at FrontEndMasters.

What is x?

function funky(o) { o = null; } var x = []; funky(x); alert(x);

o is a parameter …

Read Article

this in JavaScript

All you ever need to know about this is that this is the current context.

Ryan Morr has a good explanation of the difference between scope and context.

Every function invocation has both a scope and a context associated with it. Fundamentally, scope is function-based while context is object-based. In other words, scope pertains to the variable access of a function when it is invoked and is unique to each invocation. Context is always the …

Read Article

Variable Hoisting in JavaScript

Will this program alert? Will something be logged to the console?

 alert(5); console.log'alerted';

We are obviously missing some parentheses around the argument passed to console.log but what will that cause the browser to do? This will result in a SyntaxError. Syntax errors do not make it past the interpreter and our program does not run. It is invalid JS and nothing is alerted.

Uncaught SyntaxError: Unexpected string 

What will happen here? Anything alerted or …

Read Article