So, the heading of this chapter is “Into JavaScript”. This really takes you a bit deeper into JavaScript learning.
- Recall that there is 6 type of variables: String, Number, Boolean, null and undefined, object, symbol as already mentioned in the previous story.
The
object
type refers to a compound value where you can set properties (named locations) that each hold their own values of any type. This is perhaps one of the most useful value types in all of JavaScript.
var obj = {
a: "hello world",
b: 42,
c: function foo(){ console.log("Hello World")}
/*YES EVEN THAT
IS POSSIBLE*/
};
obj.a;//hello world
obj["b"];//42
An array is an
object
that holds values (of any type) not particularly in named properties/keys, but rather in numerically indexed positions. For example:
var arr = [
"hello world",
42,
true
];
arr[0]; //hello world
- arrays are special objects (as implies
typeof
), they can also have properties, including the automatically updated propertylength
. - Functions are a subtype of objects still returns
typeof
“function”. There are a lot of built in functions available liketoUpperCase();
,toFixed();
.Clearly, I can not state all of them.
Coercion — It is a concept where type casting of variable happens when certain operations are performed with the variable.
var a = "48";
var b = Number(a); //Normal type casting in JSvar c = a*1; // Coercion, c is a number variable
- When non boolean values are coerced to boolean then values are often judged as true and false but the question arises which one would be true and which one would be false?
- True:
"hello"(non-empty string),23(non zero number),[ ](array empty or filled),{}(object empty or filled),function foo(){___}
- False:
""(empty string),0,-0,NaN,null,undefined
Equality:
1, “==” — loose equality
2, “===” — strict equality
var a = "42",b = 42;
a==b;//true
a===b;//false
- We must avoid “==” while comparing with :
0,"",[],true/false,null
- Another interesting thing I found in this chapter was the comparison of an array. Array comparison will always give
false
. - Behavior of != and !== is loose and strict respectively. Also, inequalities are pretty much basic and are not different from the classic one (<=,>=,<,>).
Now I‘ll just write some definitions of some terms. We have dedicated chapters that will cover these topics in depth. So don’t worry if you do not get it in one go.
- Hoisting: It is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function or scope).
- Polyfiling: A polyfill is a code that implements a feature on web browsers that do not support the feature.
- Transpiling: Process of reading source code written in one programming language, and producing the equivalent code in another language.
- Prototypes: When you reference a property on an object, if that property doesn’t exist, JavaScript will automatically use that object’s internal prototype reference to find another object to look for the property on. You could think of this almost as a fallback if the property is missing.
- this identifier: If a function has a
this
reference inside it, thatthis
reference usually points to anobject
. But whichobject
it points to depends on how the function was called. It’s important to realize thatthis
does not refer to the function itself, as is the most common misconception. - Closures: You can think of closure as a way to “remember” and continue to access a function’s scope (its variables) even once the function has finished running.
- IIFEs: Immediately Invoked Function Expressions. As the name suggests these gets invoked immediately after function declaration. A common example is a settimeout function.
(function IIFE(){
console.log( "Hello!" );
})();
// "Hello!"
- Modules: Modules let you define private implementation details (variables, functions) that are hidden from the outside world, as well as a public-API that is accessible from the outside.
- use Strict: Its a good programming practice to use this as it refrains you to do logical errors by not allowing you to make global variables.
I’ve covered most of the part in this chapter. YDKJS up and going also has a chapter 3 but I would not be doing that part as its nothing but an introduction to all the book series.
So thank you for reading it. Follow my coding journey on github. If you are new then follow me on medium.