This chapter will briefly discuss some exceptions and cases of JavaScript’s syntax and behavior. As a dynamic and interpreted programming language, its syntax is different from that of traditional object-oriented programming languages. These concepts are fundamental to JavaScript and will help you to develop a better understanding of the process of designing algorithms in JavaScript.
JavaScript Scope
The scope is what defines the access to JavaScript variables. In JavaScript, variables can belong to the global scope or to the local scope. Global variables are variables that belong in the global scope and are accessible from anywhere in the program.
Global Declaration: Global Scope
However, this creates a global variable, and this is one of the worst practices in JavaScript. Avoid doing this at all costs. Always use var or let to declare variables. Finally, when declaring variables that won’t be modified, use const.
Declaration with var: Functional Scope
In JavaScript, var is one keyword used to declare variables. These variable declarations “float” all the way to the top. This is known as variable hoisting. Variables declared at the bottom of the script will not be the last thing executed in a JavaScript program during runtime.
The bottom variable declaration, which was at the last line in the function, is floated to the top, and logging the variable works.
The key thing to note about the var keyword is that the scope of the variable is the closest function scope. What does this mean?
In Java, this syntax would have thrown an error because the insideIf variable is generally available only in that if statement block and not outside it.
4 was printed, not the global value of 1, because it was redeclared and available in that scope.
Declaration with let: Block Scope
In this example, nothing is logged to the console because the insideIf variable is available only inside the if statement block.
Equality and Types
JavaScript has different data types than in traditional languages such as Java. Let’s explore how this impacts things such as equality comparison.
Variable Types
Truthy/Falsey Check
Here, node is some variable. If that variable is empty, null, or undefined, it will be evaluated as false.
false
0
Empty strings ('' and "")
NaN
undefined
null
true
Any number other than 0
Non-empty strings
Non-empty object
=== vs ==
JavaScript is a scripting language, and variables are not assigned a type during declaration. Instead, types are interpreted as the code runs.
"5" == 5 returns true because "5" is coerced to a number before the comparison. On the other hand, "5" === 5 returns false because the type of "5" is a string, while 5 is a number.
Objects
Most strongly typed languages such as Java use isEquals() to check whether two objects are the same. You may be tempted to simply use the == operator to check whether two objects are the same in JavaScript.
Although these objects are equivalent (same properties and values), they are not equal. Namely, the variables have different addresses in memory.
This is why most JavaScript applications use utility libraries such as lodash1 or underscore,2 which have the isEqual(object1, object2) function to check two objects or values strictly. This occurs via implementation of some property-based equality checking where each property of the object is compared.
Although the two functions perform the same operation, the functions have different addresses in memory, and therefore the equality operator returns false. The primitive equality check operators, == and ===, can be used only for strings and numbers. To implement an equivalence check for objects, each property in the object needs to be checked.
Summary
JavaScript has a different variable declaration technique than most programming languages. var declares the variable within the function scope, let declares the variable in the block scope, and variables can be declared without any operator in the global scope; however, global scope should be avoided at all times. For type checking, typeof should be used to validate the expected type. Finally, for equality checks, use == to check the value, and use === to check for the type as well as the value. However, use these only on non-object types such as numbers, strings, and booleans.