JavaScript and its Confusion

Primitive Value:

Md. Sazzadur Rahman
4 min readMay 6, 2021

The value, which can be assigned to another variable and that’s variable takes a copy of the value not point the address is a primitive value.

example: Strings, Numbers, Booleans, null, undefined, Symbol

typeOf:

typeOf keyword is used to know the type of variable as it is number, string, or object.

console.log(typeOf(2)); // number

console.log(typeOf(“hello”)); // String

console.log(typeOf(undefined)); // undefined

Expression:

Expression is a Javascript question that javascript can answer using value.

const addition = 2 + 5 // this is an expression

try… catch:

try… catch helps to catch and modify the error if any errors occurred in code. Code starts in try{} block, if there is an error then it goes to catch. Otherwise catch(error){} block is omitted. try…catch only work run time environment like in JavaScript. It works synchronously. if any setTimeOut() function is written in try{} block, it omitted catch(error) for setTimeOut() function. Catch() blocks create an error as an object where there are error names, error messages, and stacks → where the error has occurred. if any code is written in finally{} block, it always executes if an error occurred or not.

try {

console.log(“Execution Start”);

}

catch (err) {

console.log(“Die if there is an error”);

}

finally {

console.log(“… execute always …”) ;

}

Variable Declarations

Var:

Variable declare with var is always function and global scope.

var name = “Rahman”;

function myName(){

var name = “sazzad”;
console.log(name);
}
myName();

You can redeclare a variable more than once.

var name = “Rahman”;

var name = “sazzad”;

function myName(){

console.log(name);
}
myName();

If a variable declares in the function it cannot be accessible from outside of the function.

function myName(){
var name = “sazzad”;
}
console.log(name); // output: undefined or reference error.
myName();

A variable is accessible from outside of an if condition if the condition is matched.

if(1){
var games = [“football”, “cricket”, “kabadi”];
}

function sports(){
console.log(games) // [“football”, “cricket”, “kabadi”]
}
console.log(games);
sports() // [“football”, “cricket”, “kabadi”]

let:

If let is declared outside of a function is always global and declaration inside a function is local. If it is declared into the function it cannot be accessible from outside of a function. You cannot declare the variable using let more than one in globally. Its behavior is known as block scope {}.

Example 1:

let name = “sazzad”; // Global

function myName(){
console.log(name);
}
myName();

Example 2:

function myName(){
let name = “sazzad”; // local variable
}
console.log(name); // output: undefined

variable declares in if condition cannot accessible from outside of the condition.

if(1){
let games = [“football”, “cricket”, “kabadi”];
}

function sports(){
console.log(games) // reference error
}
console.log(games);
sports() // reference error

const: its role almost the same as the let, but you cannot declare it and put data into it more than once.

Hoisting:

In the compile phases, variable and function declaration put into the memory instead of physically move them to the top of the code, their position is remaining unchanged. The main advantages of putting function declaration into the memory are it can be accessed from anywhere in the code before it can be declared.

myName(“Sazzad”)

function myName(name){
console.log(`My Name is ${name}`)
}

//output: My Name is Sazzad

Hoisting happens with the other data types and variables. If a variable declared before initialization then “only declarations are hoisted”.

console.log(name); // output: undefined

var name;

name = “sazzad”

in the above code only declarations are hoisted;

console.log(name); // output:ReferenceError

var name = “sazzad”;

The above example has only initialization. That’s why no hoisting happens and gives the reference error. This rules are also applicable for let and const.

Functions with Default Parameter Values

Function able to initialize a default value if any data or value is not passed.

function addition(num1, num2 = 5) {
console.log(num1 + num2)
}

addition(5); // output: 10

if data passed using a parameter then the default value is not working. It is working for an undefined variable.

function addition(num1, num2 = 3) {
console.log(num1 + num2)
}

addition(5, 10); // output: 15

Arguments:

Arguments return the parameter as an array inside the function.

function data(allData){
console.log(arguments)

// output: Arguments(2) [“Sazzad”, “Onik”, callee: ƒ, Symbol(Symbol.iterator): ƒ]
}

data( “Sazzad”, “Onik”);

Spread Operator

Spread Operator allows elements of an array, or string to iterate.

let msg = [“Hi”, “there”];
console.log(…msg); // output: Hi there

const info = “Hello world”;
console.log(…info); // output: H e l l o w o r l d

Block-level functions:

The block-level function allows a function to accessible into a block. It cannot be called outside of the block

{
function greetings(){
console.log(“Hello World”); // output generates error
}
}

greetings();

Arrow Functions:

arrow function is an updated version of the previous function which can stored in a variable and it has so many shortcut techniques. It helps developers to write cleaner code.

const games = [“football”, “cricket”, “kabadi”];
games.forEach(game => console.log(game));

//> “football” > “cricket” > “kabadi”

--

--