• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

The Coding Couple

Pair programming is a lifetime commitment.

  • Home
  • Categories
    • Arduino
    • JavaScript
    • Python
    • Raspberry Pi
  • About Us
    • Privacy Policy
  • Contact

3 JavaScript Gotchas for the C# Developer

April 19, 2015 by Ashley

Most of my development experience has been with C#.   When I started learning JavaScript, I made a few poor assumptions about the language.  My incorrect assumptions about JavaScript inspired this list of three JavaScript gotchas for the C# developer:

  1. Brackets do not define scope.
  2. this does not refer to an instance of a class.
  3. Function overloading doesn’t work quite like you’d expect it to.

A quick disclaimer.  I orignally created this list of gotchas prior to learning ES2015.

Brackets do not define scope

In C#, defining new scope is as simple as enclosing code within braces (block scoping).  In JavaScript, scope is not defined by braces.  Scope is defined by functions.  Consider the following snippet:

function sum(numbers) {
    var sum = 0;
    for (var x = 0; x < numbers.length; x++) {
        sum += numbers[x];
    }
    console.log(x); // prints numbers.length - 1
    return sum;
}

It is legal to access the variable x after the for loop.  JavaScript uses a mechanism known as hoisting for its functions and variables.  Declarations are hoisted or moved to the top of the function or global code.  In our case, the variable x is hoisted to the top of the function.  The actual snippet runs like this:

function sum(numbers) {
    var sum = 0;
    var x;
    for (x = 0; x < numbers.length; x++) {
        sum += numbers[x];
    }
    return sum;
}

ES2015 introduced the keywords let and const.  Unlike variables declared with var, let and const variables have block scope.  Variables declared with let and const are still hoisted, but the variables are hoisted to the top of the block versus to the top of the function.

If we rewrote our sum function with let, accessing the variable x after the for loop would result in a ReferenceError.

function sum(numbers) {
    let sum = 0;
    for (let x = 0; x < numbers.length; x++) {
        sum += numbers[x];
    }
    console.log(x); // ReferenceError: x is not defined
    return sum;
}

An update:

I learned something new related to scoping in JavaScript: variables declared with var in the catch block of a try/catch statement have block scope.

For a more thorough explaination of scope in JavaScript, check out Kyle Simpson’s book  You Don’t Know JS: Scope & Closures.

this does not refer to an instance of a class

In C#, the this keyword returns the current instance of a class.  In JavaScript, this behaves a bit differently.  If you access this outside of a function, this refers to the global object.  If you access this within a function the this keyword references the caller of the function.

Let’s return to our sum function.  Accessing this within the function will return window, our global object.

function sum(numbers) {
    let sum = 0;
    for (let x = 0; x < numbers.length; x++) {
        sum += numbers[x];
    }
    console.log(this); // logs the 'Window' object
    return sum;
}

If sum was a property on an object, this would reference that object.

var math = Object.create(null);
math.sum = sum;
math.sum(1, 2, 3); // console logs 'Object {}'

It is important to understand how this behaves. The this keyword can be tricky to use in a callback, especially when passing it into a third party library.

Function overloading doesn’t work quite like you’d expect it to

In JavaScript, if you tried to overload a function the same way you would in C#, you are overwriting the previously defined function.  Take a look at the following snippet:

function add(x, y) {
    return x + y;
}

function add(x, y, z) {
    return x + y + z;
}

The second function declaration will overwrite the previous one.  The call add(1, 2) will return NaN instead of the value 3.

If you want to create a function with overrides, you can make use of the arguments array.  To support function overloading, we can rewrite the snippet above as such:

function add() {
     if(arguments.length === 2) {
         return arguments[0] + arguments[1];
     } else if (arguments.length === 3) {
         return arguments[0] + arguments[1] + arguments[2];
     }
}

(It’s not a best practice to use the arguments array.)

 Recommended Reading

Disclosure: Some links are affiliate links.

Here’s a list of resources I found very useful in my journey with JavaScript:

  • Mozilla Developer Network (MDN) JavaScript Reference
  • Secrets of the JavaScript Ninja
  • Effective JavaScript
  • You Don’t Know JS (book series)

Related Posts

  • Pokémon Color Picker | A web app built with HTML/CSS + JavaScriptPokémon Color Picker | A web app built with HTML/CSS + JavaScript
  • .mjs extension (A JavaScript module file) | Today I Learned.mjs extension (A JavaScript module file) | Today I Learned
  • Getting started with Phaser and ES2015Getting started with Phaser and ES2015
  • Surviving the Hackathon:  Angular Attack 2016Surviving the Hackathon: Angular Attack 2016
  • Surviving the Hackathon:  Ludum Dare 35Surviving the Hackathon: Ludum Dare 35
  • Southeastern Technical Conferences & Events (2016 Edition)Southeastern Technical Conferences & Events (2016 Edition)

Filed Under: JavaScript Tagged With: brackets, c#, developer, function overloading, gotcha, hoisting, javascript, scope, this

Previous Post: « 5 Ways to Improve Your Debugging Skills
Next Post: There’s a name for that: the Kebab Case »

Primary Sidebar

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Recent Posts

  • Pokémon Color Picker | A web app built with HTML/CSS + JavaScript
  • Pokéball Single DIV CSS Drawing | Tutorial
  • Error: [🍍]: “getActivePinia()” was called but there was no active Pinia
  • Trijam #261 Game Jam Diary: One Wrong Move
  • Using WSL on Corporate VPN

Recent Comments

  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley Grenon on Creation Crate Month 2: An Arduino Powered Memory Game
  • Lizzy on Creation Crate Month 2: An Arduino Powered Memory Game
  • kelly on Creation Crate Month 2: An Arduino Powered Memory Game
  • Ashley on Creation Crate Month 3: An Arduino Powered Distance Detector

Follow us on Instagram!

This error message is only visible to WordPress admins

Error: No feed found.

Please go to the Instagram Feed settings page to create a feed.

Categories

  • Arduino
  • Conferences
  • Debugging
  • Game Jams
  • HTML and CSS
  • JavaScript
  • Programming Languages
  • Python
  • Raspberry Pi
  • Today I Learned

Archives

  • May 2024
  • April 2024
  • March 2024
  • May 2022
  • December 2021
  • May 2021
  • March 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • June 2019
  • April 2019
  • September 2017
  • April 2017
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • April 2015
  • January 2015

Tags

adafruit arduino brackets c# code smell codestock coding standards conventions creation crate debugging developer devspace electronics es6 es2015 game development game jam gotcha hackathon hoisting html html5 javascript led naming conventions nintendo phaser pluralsight pokemon programmer python raspberry pi retro retropie scope self improvement single div single div drawing subscription box TIL today I learned troubleshooting vue vuejs windbg

Footer

About Us

We are the Coding Couple.  Two people who met in college and decided they wanted to pair program for the rest of their ...

Read More »

Most Recent Posts

Pokémon Color Picker | A web app built with HTML/CSS + JavaScript

Pokéball Single DIV CSS Drawing | Tutorial

Error: [🍍]: “getActivePinia()” was called but there was no active Pinia

Trijam #261 Game Jam Diary: One Wrong Move

Social Media

  • GitHub
  • Instagram
  • Twitter
  • YouTube

Copyright Notice

© The Coding Couple, 2015 – 2023. Excerpts and links may be used, provided that full and clear credit is given to The Coding Couple with appropriate and specific direction to the original content.

Copyright © 2025 · Foodie Pro Theme by Shay Bocks · Built on the Genesis Framework · Powered by WordPress