So You Want to Use JavaScript in The Next Freelance Project — Some Basics

I still remember the days of debugging CORS problem when I put together some projects using JavaScript (& ajax), “a very particular programming language” in my first impression. Recently I got a great opportunity. The new role uses JS, the browser-side script that is now winning in all sides, as the major language. So I took it as a good chance to learn JS more systematically, and this series will be part of the outcome of my study. As the name implies, I will not cover primary level such as “if, else” (condition), “for” (or any kinds of loops), or basic OOP concepts. Instead, I will focus only on differences so you can learn this versatile language like reviewing a pull request, and use it the next day in your next awesome project.
— Some Basics
— String and Array
— This
— Object (Dictionary)
— Prototype (1)
— Prototype (2)
— Async
By the way, CORS was solved using Moesif Origin & CORS Changer.
Comparison, == V.S. ===
==
does automatic type conversion and compare the “value”. So a string "1"
equals to an integer 1
in ==
.
===
does not do type conversion. So when comparing a string to an integer, ===
returns false
regardless of the value.
Example:
//comparing string with number
var res = "1" == 1;
alert(res); //=>trueres = "1" === 1;
alert(res); //=>false//comparing two special type with the same value
res = null == undefined;
alert(res); //=>trueres = null === undefined;
alert(res); //=>false//comparing number with object
var obj = new Number(123);
res = 123 == obj
alert(res) //=>trueres = 123 === obj
alert(res) //=>false
Result:
true
false
true
false
true
false
It is worth noting that the result of NaN === NaN
is always false
. Use isNaN()
in such case.
The complete behavior of these two “equal-to” operations is given by the equality tables below, use this cheat sheet when something counter-intuitive happens:


Source: github.io
NaN, undefined and null
Simply put,
undefined
is a special init value assigned to a variable (implicitly) by the runtime;null
is a value assigned by a programmer to mark the variable as “I am done with the it”; andNaN
is set to indicate that something goes wrong (again, by the runtime).
Practically,
- look at the “declaration” portion of your code when encountering
undefined
, the uninitialized; - search for
= null
and evaluate the occurrences in your project when encounteringnull
, the released; and - look inside a function implementation and check if the arguments are passed correctly when encountering
NaN
, an error.
I think subdividing of these exceptional “nil”s can narrow down the code review scope for bug analysis. Do you think so too? Let me know in the comment bellow.
Example:
var res;
alert(res);res = null;
alert(res);res = parseFloat("geoff");
alert(res);res = 1/0;
alert(res);
Result:
undefined
null
NaN
infinity
It is worth noting that n/0
does not make a NaN
, as some claims.
The flexibility of JavaScript introduces cases of uninitialized variables (undefined
) that are not common in other languages.
Following are the examples:
var res = [1,2,3];
alert(res[10]);function doSomething(first, second, optional) {
alert(optional);
}doSomething(1,2);
Result:
undefined
undefined
As demonstrated by the code, whenever a variable is not explicitly assigned with a value, which could be the case of an out-of-range array accessing or an ignored function argument, the variable is set as undefined
by the runtime.
Thanks for the reading and I’ll see you next time.