Primitive types
Undefined | NULL |
---|---|
The undefined property indicates that a variable has not been declared at all. | The value null represents the absence of any object value |
Undefined Example
var demo;
alert(demo); //shows undefined
alert(type of demo); //shows undefined
NULL Example
var demo = null;
alert(demo); //shows null
alert(typeof demo); //shows object
When to use null object?
Can we assign undefined to variable?
So the summary is undefined is #javascript way of saying that you forgot to initialize something or it is not existent (assigned by js) and null is user way of saying that I have initialized and want the variable to be non existent (assigned by user).
| == | === | | —————————————————————————————————- | ———————————————————————————————– | | returns true if operands have the same data type and same value, returns false if the values differ. | returns true only if operands are of the same data type and same value, otherwise returns false | | Also known as loose equality | Also known as strict equality |
== Example
var x = 2;
x == y; // Returns true since the value of both x and y is the same
=== Example
var y = "2";
x === y; // Returns false since the typeof x is "number" and typeof y is "string"
var | let | const |
---|---|---|
The scope of a var variable is functional scope. | The scope of a let variable is block scope. | The scope of a const variable is block scope. |
It can be updated and re-declared into the scope. | It can be updated but cannot be re-declared into the scope. | It cannot be updated or re-declared into the scope. |
It can be declared without initialisation. | It can be declared without initialisation. | It cannot be declared without initialisation. |
It can be accessed without initialization as its default value is “undefined”. | It can be accessed without initialization as its default value is “undefined”. | It cannot be accessed without initialisation, as it cannot be declared without initialisation. |
NaN property represents the “Not-a-Number” value. It indicates a value that is not a legal number.
isNaN("Hello"); // Returns true
isNaN(345); // Returns false
isNaN("1"); // Returns false, since '1' is converted to Number type which results in 0 ( a number)
isNaN(undefined); // Returns true
The “this” keyword refers to the object that the function is a property of. The value of the “this” keyword will always depend on the object that is invoking the function.
var obj = {
name: "Vivek",
getName: function () {
console.log(this.name);
},
};
obj.getName();
JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, before execution of the code.
Hoisting allows functions to be safely used in code before they are declared.
Without Hoisting
function catName(name) {
console.log(`My cat's name is ${name}`);
}
catName("Tiger");
/*
The result of the code above is the same: "My cat's name is Tiger"
*/
With Hoisting
catName("Tiger");
function catName(name) {
console.log(`My cat's name is ${name}`);
}
/*
The result of the code above is: "My cat's name is Tiger"
*/
A closure is simply a function inside another function with access to the outer function variable.
In other words, closure is created when a child function keeps the environment of the parent scope even after the parent function has already been executed
In javascript, a callback is simply a function that is passed to another function as a parameter and is invoked or executed inside the other function.
We understand the concept of callback but what will happen if your code will have callbacks within callbacks within callbacks and it goes on. Well, this recursive structure of callback is called ‘callback hell’ and promises to help to solve this kind of issue. Promises are useful in asynchronous javascript operations when we need to execute two or more back-to-back operations (or chaining callback), where each subsequent function starts when the previous one is completed. A promise is an object that may produce a single value sometime in the future, either a resolved value or a reason that it’s not resolved (rejected)
A promise may be in three possible states…
We can consume any promise by attaching then() and catch() methods to the consumer.
Stop and wait until something is resolved. Async & await just syntactic sugar on top of Promises and like promises it also provides a way to maintain asynchronous operation more synchronously. So in javascript asynchronous operations can be handled in various versions…
You can use Async/Await to perform the Rest API request where you want the data to fully load before pushing it to the view. For Nodejs and browser programmers async/await is a great syntactic improvement. It helps the developer to implement functional programming in javascript and it also increases the code readability.
Recursion is a technique to iterate over an operation by having a function call itself repeatedly until it arrives at a result.
let myFunction = (a, b) => a * b;
Keyword | const | let | var |
---|---|---|---|
global scope | no | no | yes |
function scope | yes | yes | yes |
block | yes | yes | no |
can be reassigned | no | yes | yes |
An array is a collection of the same data type elements
There are 4 ways to write Functions in JavaScript
// Function Declaration
function square(x) {
return x * x;
}
// Function Expression
const square = function(x) {
return x * x;
}
// Arrow Function Expression
function square =(x)=> {
return x * x;
}
// Concise Arrow Function Expression
function square = x => x * x;