Skip to main content

Back to Basics

· 3 min read

When I started my formal education in computer science, I had to take a module on programming with the C language. I remember this was a module that caused many of my peers to re-think their decision to study computer engineering; the concept of pointers was so foreign to many of them that even the smart ones scored poorly.

Memory-managed programming languages like Java and PHP have made memory pointers much less important. For better or worse, programming has become a much less daunting endeavour now.

As a regular JavaScript writer, I have become used to the affordances that the myriad array of frameworks have provided in terms of convenience and abstraction from the nitty-gritty parts of the language. Instead of writing "straight-up" JavaScript, I just call a bunch of functions here and there and get the job done in half the time. So this post is an exercise to recap the basics of JavaScript.

Just in case you, dear reader, are new to JavaScript, it is a loosely-typed language. This means that a variable can hold any type of data.

The types of data in JavaScript are:

  • string
  • number (integer, float)
  • boolean
  • function
  • object/array
  • null
  • undefined

The first three (string, number and boolean) are primitive types of the JavaScript language. You never have problems with straight-up variable assignment with them.

var a = 'string';
var b = 1;
var c = b;

b = a;
console.log(a); //'string'
console.log(b); //'string' because of b = a
console.log(c); //retains 1

So a variable can be assigned one type (integer) and re-assigned to another (string) with no confusion.

In a straight-up assignment, you expect b to be updated with the value of a.

What is not clear to beginners is since c is assigned the value of b, does changing the value of b affect the value of c?

The answer is no because when an assignment takes place in JavaScript, the contents are copied over to the assigned variable.

In a language with pointer arithmetic (such as C), things will be different, and a little complicated, when pointers come into the picture.

Things become interesting when you start working with contents of arrays.

Let's see what happens if the value of the array is assigned to another variable.

var a = [1, 2];
var b = a;
var c = a[0];
b[0] = 3;
console.log(a); //shows [3, 2]
console.log(b); //shows [3, 2]
console.log(c); //shows 1, not 3

If you are expecting c to show the value of 3, you are not alone.

What happens here is that when the assignment of c happened, a copy of the value in b[0] was made and assigned to c.

When b was assigned a, a copy of the location of the array in memory was made and assigned to b. Thus, when b and a are referenced, they both point to the same location in memory i.e. the same array.

In summary, the difference between the examples lies in the fact that with array values, we are essentially working with memory locations as compared to actual values.

I'm deferring the use of the terms "pass-by-reference" and "pass-by-value" here because, like politics, everyone has his/her own opinions, and everyone disagrees with everyone else's views. Things become even more complicated when you are talking about a language that does not have direct memory arithmetic.