Skip to main content

10 Javascript interview questions


  1. What's the difference between let, var and const in JavaScript?

    var is function scoped and it can be re-declared and updated. let and const are block scoped, which means they are only accessible within the block they are defined in. let can be updated, but not re-declared. const is used for variables that shouldn't be reassigned.

  2. How do you create a function in JavaScript?

    Functions can be created using the function keyword, followed by the name of the function, a set of parentheses and a set of curly braces. Example:
    function exampleFunction() {
         // function code here
    }


  3. Can you explain the difference between == and === in JavaScript?

    == compares values for equality and will perform type coercion if needed. === compares values and their type, and will only return true if the values and types are the same.


  4. What is Hoisting in Javascript?

    In JavaScript, hoisting is the behavior where variable and function declarations are moved to the top of their scope at the time of execution. This means that variable declarations using the var keyword are hoisted to the top of the scope and initialized with a value of undefined, but their assignments are not moved.
    Function declarations are also hoisted to the top of the scope. Similarly, variable declarations using let and const are also hoisted to the top of their scope, but they are not initialized and trying to access them before they have been initialized will result in a ReferenceError.

    It's important to note that when you declare a variable using let or const, they are not accessible until the interpreter reaches the declaration in the code, this state is called Temporal Dead Zone (TDZ). The variable is in the TDZ from the start of the block until the declaration is encountered.

  5. Can you explain closure in JavaScript?

    A closure is a function that has access to variables in its parent scope, even after the parent function has returned. It allows a function to "remember" its state, even after it has completed its execution.

  6. Can you explain the difference between synchronous and asynchronous code in JavaScript?

    Synchronous code is executed one line at a time, in the order it is written. Asynchronous code can run multiple tasks at the same time, rather than waiting for one task to complete before starting another.

  7. Can you explain the event loop in JavaScript

    The event loop is a mechanism in JavaScript that allows the execution of code to be scheduled and managed as a queue of messages (or events) to be processed. The event loop processes the message queue and starts executing the code, when the stack is empty.

  8. Can you explain the difference between a promise and a callback in JavaScript?

    A callback function is a function that is passed as an argument to another function, which will be called when a certain event occurs. A promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

  9. Can you explain the difference between a class and an object in JavaScript?

    A class is a blueprint for creating objects, it defines the properties and methods that the object will have. An object is an instance of a class, it is a specific implementation of a class, with its own set of properties and methods.

  10. How can you implement a stack data structure in JavaScript?

    A stack can be implemented using an array with the push and pop methods to add and remove elements from the top of the stack, respectively, and the length property to check the size of the stack.

    const stack = [];
    stack.push(1); // [1]
    stack.push(2); // [1, 2]
    stack.pop(); // 2, [1]
    stack.length; // 1

Comments

Popular posts from this blog

Quick Tip - Accessing Dell's recovery partition

Preface: Hi, Today's quick tip is about accessing Dell's recovery partition. How many times did you felt like your computer has gone mad due to viruses or the extra load of software that you have loaded up over a period of time. I used to wonder how do we access Dell's recovery partition as its not available through some obvious means. I just learned how to do this so sharing it with the people so that it may save someone some time. The Tip: To access Dell's recovery partition, you need to press Ctrl+F11 right after turning the machine on. If it do not show up by pressing once, you may repeat the key sequence "gently". Once its gone beyond the BIOS screen and started to load the OS, means that you missed the spot and you need to restart the computer again to go through the same cycle again. After successfully invoking Dell System Recovery , it will show up the default drive image and will provide you an option to refresh you computer to factory defaults.

Top 10 Javascript (ES6+) Interview Questions with sample code

Welcome to the world of JavaScript interview questions! If you're preparing for a job interview as a JavaScript developer, you've come to the right place. We've compiled a list of the most common questions that you may encounter during your interview, along with some tips and sample answers to help you showcase your skills and knowledge. Whether you're an experienced developer or just starting out, this guide will provide you with a comprehensive understanding of the concepts and best practices of JavaScript. So, grab a cup of coffee, get comfortable, and let's dive into the world of JavaScript interview questions!  What is the difference between let and var in JavaScript? var is function scoped, whereas let is block scoped. This means that a variable declared with var can be accessed within the entire function, whereas a variable declared with let can only be accessed within the block in which it was declared. Example: //var function testVar() { var x =