Skip to main content

Command Palette

Search for a command to run...

Important Javascript Concepts ?

Published
3 min read
S

I love learning about technology and sharing that with others

  1. What's the difference between event.preventDefault() and event.stopPropagation() methods?

  2. The event.preventDefault() basically prevent the default behavior of the element in case it is used with form it will prevent it with submit. if used with anchor it will prevent it from navigating.

event.stopPropogration() --> prevent the event from bubbling and capturing phase.

  1. What is event.target ?

event.target tell that on which element the event is ocuured.

 function clickFunc(event) {
  console.log(event.target);
}

here once the button is clicked this function will be called and it will log the element so everything about the particular element will be logged

  1. What's the difference between == and === ?

== compare values === compare value and type of the variable.

  1. Why does it return false when comparing two similar objects in JavaScript?

  2. javascript treat the primitive and the object type comparison differently so for the primitive type it compares using value and for the object type it compares using the reference variable means the address int he memory


       let a ={
        a:"1",
        b:"2"

      }

      let b= {
        a:"1",
        b:"2"

      }

      let c=a;

      console.log(a==b)    --> it compares the address not the value for non primitive type   --> output --> false
      console.log(a==c)   --> here it cmpares the address that's why true



    }
  1. What is Hoisting?

when the javascript is given to the javascript engine , there is something called as execution context takes place that is divided into two parts creation and updating,

so in the creation phase it will put all the function declaration and var declaration to the top and then in the execution phase value is a assigned to them.

lets take an example


console.log(y);
y = 1;
console.log(y);
console.log(greet("Mark"));

function greet(name){
  return 'Hello ' + name + '!';
}

var y;
``

output will be : 1 Hello Mark!

how it works is like this

during compilation or creation phase :

```javascript

function greet(name) {
  return 'Hello ' + name + '!';
}

var y; //implicit "undefined" assignment

//waiting for "compilation" phase to finish

//then start "execution" phase
/*
console.log(y);
y = 1;
console.log(y);
console.log(greet("Mark"));
*/

during execution phase

function greet(name) {
  return 'Hello ' + name + '!';
}

var y;

//start "execution" phase

console.log(y);
y = 1;
console.log(y);
console.log(greet("Mark"));

Note: only function declarations and variables declared with the var keyword are hoisted not function expressions or arrow functions, let and const keywords.

  1. What is Scope?

if var is declarared in global scope it can be accessed anywhere in the program

if it is declared inside method it can be accessed inside that method only

if block scoped for let and const that are scoped based on the curly braces so can only be used inside that.

  1. What are Closures?

A scope created when the function is declared

More from this blog

H

hashcodehub

271 posts

Consistent, Passionate and Organized :)