Javascript important core concepts
- Truthy vs Falsy Values
In javascript truthy value is a value that is considered true and falsy value is a value that is considered falsy value.
- All positive and negative value is truthy value without 0 (0 is falsy value)
- All string is true without empty(‘’) string (empty string is false value)
- Empty array [] and empty object are a true value
- false is a falsy value but “false” is the true value
- All undefined value is false
- Null is a falsy value
- NaN is a falsy value
Some example are given below-
Truthy value
if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
Falsy value
if (false)
if (null)
if (undefined)
if (0)
if (-0)
if (0n)
if (NaN)
if ("")
2. undefined vs null
- If a variable is declared but the value is not defined then its output is undefined
let name;
console.log(name); //Output: undefined
2. If declare a function and pass parameters but arguments are not set then its output is undefined and also value is not returned in the function then its output is also undefined
function add(num1, num2){
console.log(num2);//undefined
}
const result = add(10);
console.log(result);//undefined
3. If declare an object and set some properties but console a property which is not assigned in the object then its output is undefined
const obj = {name:”milton”, age:25};
console.log(obj.height)//output:undefined
4. null is an empty value that does not exist in the variable
const name = null;
console.log(name);//output:null
3. Double equal (==) vs Triple equal (===)
- Double equal compare the only value
20==”20” //This is true
2. Triple equal compare value and data type
20===”20" //This is false
4. Scope, Block scope
Scope: Javascript has a function scope. Scope means that if a variable is declared, where the variable is declared, where its exception can be taken. Children function can access any value from the parent function but the parent can’t access from children function.
Block scope: A block scope is the area within if, switch conditions or for and while loops. whenever you see {curly brackets}, it is a block. In ES6, const and let keywords declare variables in the block scope, which means those variables exist only within the corresponding block.
5. Closure: When returning any function into another function that is called closure and another explanation is when calling a function into another function that is called closure.
6. bind, call and apply
bind: The bind()
method creates a new function that, when called, has its this
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
fn.bind(thisArg[, arg1[, arg2[, ...]]])
call: The call()
method calls a function with a given this
value and arguments provided individually. On the other hand the call()
method, you can write a method that can be used on different objects.
fn.call(thisArg, arg1, ... , argN)
apply: With the apply()
method, you can write a method that can be used on different objects and its arguments are passing into an array after the first arguments.
fn.apply(thisArg, [arg1, ... , argN])
7. window, global variable, and global scope
window: The window object represents an open window in a browser. There is no public standard that applies to the Window object, but all major browsers support it.
global variable: The var
statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
global scope: A variable declared outside a function, becomes GLOBAL. All scripts and functions on a web page can access it.
8.DOM: DOM means Document Object Model. It is an API which defines the logical structure of documents and the way document is accessed and manipulated. Dom creates a structure that seems like a Tree, and it represents the elements of an HTML file. The purpose of doing this is, to detect user interactivity as well as to Use JavaScript to manipulate the Elements in the runtime.
9. API — GET, POST: The meaning of API is Application Programming Interface. API is a way of fetching data from the server-side and show those data to the client-side. There are several ways to request the data to serve. Which are known as GET, POST, PUT, PATCH, DELETE requests. If a developer wants to get the data he needed to perform a GET request. If he wants to store some data or send some data to the server he can perform it through the POST, PUT, or PATCH method.
10. Arrow function: An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations.