In this article, we will focus specifically on things you need to know about JavaScript to get up and going as a JS developer.
JavaScript is dynamic typed, meaning variables can hold values of any type without any type enforcement. The following build-in types are available :
JavaScript has typed values, not typed variables, that means a variable in JavaScript can contain any data. A variable can at one moment be a string and at another be a number:
var a;
typeof a; // "undefined"
a=5;
typeof a; // "number"
a="js"
typeof a; // "string"
typeof null is an interesting case because it errantly returns “object” when you’d expect it to return “null”.
Objects are used to store keyed collections of various data and more complex entities, where you can set properties that hold their own values.Properties can either be accessed with dot notation (i.e., obj.a) or bracket notation (i.e., obj[“a”]). Dot notation is shorter and generally easier to read and is thus preferred when possible.
var obj = {
a: "js",
b: 2
}
obj.a; // "js"
obj["b"]; // 2
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 = ["js", 2, true];
arr[0]; // "js"
arr[1]; // 2
arr[2]; // true
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript:
When can have properties and method on non-primitive value.But how can we length as property and, toUpperCase() as a method on primitive value?
const a = "hello world";
a.length; // 11
a.toUpperCase(); // "HELLO WORLD"
typeof a; // "string"
When you use a primitive value like “a” as an object by referencing a property or method (e.g., a.toUpperCase() in the previous snippet), JS automatically “boxes” the value to its object wrapper counterpart (hidden under the covers). A string value can be wrapped by a String object, a number can be wrapped by a Number object, and a boolean can be wrapped by a Boolean object.
“Boxing” is wrapping an object around a primitive value. For instance,new Number(42) creates a Number object for the primitive number 42.
The specific list of “falsy” values in JavaScript is as follows:
• “ “ (empty string)• 0, -0, NaN (invalid number)• null, undefined• false
Any value that’s not on this “falsy” list is “truthy.” For example : [ ], [1,2,3],{}, “hello”, 2, {a: 7}.
There are four equality operators: ==, =,(Equality) and !=, ! (inEquality).
The difference between == and === is usually characterized that == checks for value equality and === checks for both value and type equality. However, this is inaccurate. The proper way to characterize them is that == checks for value equality with coercion allowed, and === checks for value equality without allowing coercion;=== is often called “strict equality” and == is “loose equality” for this reason.
Coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on). It comes in two forms : explicit an implicit.
Example of explicit coercion:
var a = “42”;
var b = Number( a );
a; // “42”
b; // 42 — the number!
Example of implicit coercion:
var a = “42”;
var b = a * 1; // “42” implicitly coerced to 42 here
a; // “42”
b; // 42 — the number!
The <, >, <=, and >= operators are used for inequality, referred to in the specification as “relational comparison.”They will be used with comparable values like numbers. But JavaScript string values can also be compared for inequality.
var a = 42;
var b = "foo";
var c = "53";
var d = "boo" ;
a < c // true or false ?
a < b; // true or false ?
a > b; // true or false ?
a == b; // true or false ?
b > d; // true or false ?
What should be the answer to the above inequality expression? False or True ??Before finding the solution of the above, let see how two string or number and string are compared.If both values in the < comparison are strings, as it is with b > d, the comparison are made lexicographically (aka alphabetically like a dictionary).Similar to the equality operator, coercion is applied to the inequality operators as well. If one or both is not a string, as it is with a < b, then both values are coerced to be numbers, and a typical numeric comparison occurs.
a < c // true convert "53" to 53, 42 < 53
a < b; // false convert "foo to NaN, 42 < Nan
a > b; // false convert "foo to NaN, 42 >Nan
a == b; // false interpreted as 42 == NaN or "42" == "foo"
b > d; // true f come after b in alphabetic order
When converting “foo” into a number we get “invalid number value” NaN and NaN is neither greater than nor less than any other value.
You use the var keyword to declare a variable that will belong to the current function scope, or the global scope if at the top level outside of any function.
We have two more keywords for variable declaration: let and const. They are block scope.
Hoisting
When JavaScript compiles all of your code, all variable declarations using var are hoisted/lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made. This is what we mean by “hoisting”.
Functions declarations are also hoisted, but these go to the very top, so will sit above all of the variable declarations.
console.log(myName);
var myName = ‘Sunil’;
What will be the answer -
1. Uncaught ReferenceError: myName is not defined
2. Sunil
3. undefined
In the above example, the answer will be — undefined. Why ?? As we mentioned earlier, variables get moved to the top of their scope when your Javascript compiles at runtime. So when the Javascript gets compiled, var myName would get moved to the top of its scope.
The only thing that gets moved to the top is the variable declarations , not the actual value given to the variable.
So that’s why we get undefined, not a ReferenceError because variable declared at the top of the scope.
Closure is one of the most important, and often least understood, concepts in JavaScript. A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in the parent function scope.
The closure has access to the variable in three scopes:
Variable declared in his own scope
Variable declared in parent function scope
Variable declared in the global namespace
function makeAdder(x) {// parameter x
is an inner variable
// inner function add()
uses x
, so// it has a "closure" over itfunction add(y) {return y + x;};
return add;
}
var plusTen = makeAdder( 10 ); plusTen( 3 ); // 13 <-- 10 + 3 plusTen( 13 ); // 23 <-- 10 + 13
When we call makeAdder(10), we get back a reference to its inner add(..) that remembers x as 10. We call this function reference plusTen(..) it adds its inner y( 3 ) to the remembered by x( 10 ).
Another very commonly misunderstood concept in JavaScript is the this keyword. If a function has a this reference inside it, that this reference usually points to an object. But which object it points to depends on how the function was called. It’s important to realize that this does not refer to the function itself, as is the most common misconception.
function foo() {
console.log( this.bar );
}
var bar = "global";
var obj1 = {
bar: "obj1",
foo: foo
};
var obj2 = {
bar: "obj2"
};
foo(); // global
obj1.foo(); // "obj1"
foo.call( obj2 ); // "obj2"
new foo(); // undefined
To understand what this points to, you have to examine how the function in question was called. It will be one of those four ways just shown above, and that will then answer what this is.
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.The internal prototype reference linkage from one object to its fallback happens at the time the object is created. The simplest way to illustrate it is with a built-in utility called Object.create(..).
var foo = {
a: 42
};
// create `bar` and link it to `foo`
var bar = Object.create( foo );
bar.b = "hello world";
bar.b; // "hello world"
bar.a; // 42 <-- delegated to `foo`
All the terms we have discussed till now (are in brief ) to become a good JS developer you need to become familiar with these terms.You can start reading the official JS documentation to learn all these concepts in dept.
Follow me on Twitter, LinkedIn or GitHub.
I hope this article is useful to you. Thanks for reading & Keep Learning !!