<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oluwagbenga</title>
    <description>The latest articles on DEV Community by Oluwagbenga (@kosoko_daniel).</description>
    <link>https://dev.to/kosoko_daniel</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F541714%2F948f3ca3-2b14-4768-b236-e43d3b7d5314.jpg</url>
      <title>DEV Community: Oluwagbenga</title>
      <link>https://dev.to/kosoko_daniel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kosoko_daniel"/>
    <language>en</language>
    <item>
      <title>Mastering JavaScript Interviews: 5 Common Questions and Answers</title>
      <dc:creator>Oluwagbenga</dc:creator>
      <pubDate>Wed, 22 Feb 2023 16:49:32 +0000</pubDate>
      <link>https://dev.to/kosoko_daniel/mastering-javascript-interviews-5-common-questions-and-answers-10hc</link>
      <guid>https://dev.to/kosoko_daniel/mastering-javascript-interviews-5-common-questions-and-answers-10hc</guid>
      <description>&lt;p&gt;Hey there! If you're getting ready for a JavaScript interview, chances are you'll come across some common questions. But don't worry, I've got you covered! In this post, I'll go over five of the most common JavaScript interview questions and give you detailed answers, complete with code examples and explanations. And don't worry, I'll do my best to make it easy to understand so you can ace that interview!&lt;/p&gt;

&lt;h2&gt;
  
  
  Question 1: What is closure?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; A closure is a function that has access to variables in its outer lexical scope, even after the outer function has returned. &lt;br&gt;
Here are two examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function outer() {
  const message = "Hello";
  function inner() {
    console.log(message);
  }
  return inner;
}
const myFunc = outer();
myFunc(); // output: "Hello"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;outer&lt;/em&gt; function declares a variable called message and a function called &lt;em&gt;inner&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;inner&lt;/em&gt; function has access to the message variable, even though it's declared in the &lt;em&gt;outer&lt;/em&gt; function.&lt;/li&gt;
&lt;li&gt;When the &lt;em&gt;outer&lt;/em&gt; function is called and returns the &lt;em&gt;inner&lt;/em&gt; function, the message variable is still available in memory, This is closure😊&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function counter() {
  let count = 0;
  return {
    increment: function() {
      count++;
      console.log(count);
    },
    decrement: function() {
      count--;
      console.log(count);
    }
  };
}
const myCounter = counter();
myCounter.increment(); // output: 1
myCounter.increment(); // output: 2
myCounter.decrement(); // output: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;counter&lt;/em&gt; function returns an object with two methods: increment and decrement.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;count&lt;/em&gt; variable is declared inside the &lt;em&gt;counter&lt;/em&gt; function and is accessible from both methods via closure.&lt;/li&gt;
&lt;li&gt;When the methods are called, they update the &lt;em&gt;count&lt;/em&gt; variable and log its value.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Question 2: What is the difference between var, let, and const?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; var, let, and const are all used to declare variables in JavaScript, but they have different scoping rules and usage restrictions. &lt;br&gt;
Here are two examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFunction() {
  var x = 1;
  let y = 2;
  const z = 3;
  if (true) {
    var x = 4;
    let y = 5;
    const z = 6;
    console.log(x, y, z); // output: 4 5 6
  }
  console.log(x, y, z); // output: 4 2 3
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inside the if block, the x variable is redeclared and updated with a new value using var, but y and z are redeclared and updated with new values using let and const, respectively.&lt;/li&gt;
&lt;li&gt;Outside the if block, the updated value of x is still available because it's function-scoped, while y and z retain their original values.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObj = { foo: "bar" };
myObj.foo = "baz";
console.log(myObj.foo); // output: "baz"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even though &lt;em&gt;myObj&lt;/em&gt; is declared with &lt;em&gt;const&lt;/em&gt;, the properties of the object can still be modified.&lt;/li&gt;
&lt;li&gt;However, if you try to reassign the entire object to a new value, you'll get an error because &lt;em&gt;myObj&lt;/em&gt; itself cannot be reassigned.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Question 3: What is event bubbling and how does it work?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Event bubbling is a process in which an event that is triggered on a child element is also triggered on its parent elements, all the way up the DOM tree.&lt;br&gt;
Again, here are two examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div onclick="console.log('parent element clicked')"&amp;gt;
  &amp;lt;p onclick="console.log('child element clicked')"&amp;gt;Click me&amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The HTML code defines a &lt;em&gt;div&lt;/em&gt; element with an &lt;em&gt;onclick&lt;/em&gt; attribute that logs a message when it is clicked.&lt;/li&gt;
&lt;li&gt;Inside the &lt;em&gt;div&lt;/em&gt;, there is a &lt;em&gt;p&lt;/em&gt; element with its own &lt;em&gt;onclick&lt;/em&gt; attribute that logs a different message when it is clicked.&lt;/li&gt;
&lt;li&gt;When the &lt;em&gt;p&lt;/em&gt; element is clicked, the event "bubbles up" to the &lt;em&gt;div&lt;/em&gt; element, which also logs its own message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener("click", function(event) {
  console.log(event.target);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;addEventListener&lt;/em&gt; method is used to attach a &lt;em&gt;click&lt;/em&gt; event listener to the entire document.&lt;/li&gt;
&lt;li&gt;When any element on the page is clicked, the event listener is triggered, and the &lt;em&gt;event.target&lt;/em&gt; property is logged to the console.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;event.target&lt;/em&gt; property is the element that was clicked, and it can be used to determine which element triggered the event.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Question 4: What is the difference between null and undefined?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; null and undefined are two values in JavaScript that are often used interchangeably, but they have different meanings. undefined is the value of a variable that has not been initialized, while null is a value that represents the intentional absence of any object value.&lt;br&gt;
To explain this better, here are examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let foo;
console.log(foo); // Output: undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare a variable named &lt;em&gt;foo&lt;/em&gt; without assigning any value to it.&lt;/li&gt;
&lt;li&gt;When we log the variable to the console, the value of the variable is &lt;em&gt;undefined&lt;/em&gt;, because it has not been initialized.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let bar = null;
console.log(bar); // Output: null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare a variable named &lt;em&gt;bar&lt;/em&gt; and assign the value null to it.&lt;/li&gt;
&lt;li&gt;When we log the variable to the console, the value of the variable is &lt;em&gt;null&lt;/em&gt;, which indicates that there is no object value present.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Question 5: What are callbacks in JavaScript, and how do they work?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; Callbacks are functions that are passed as arguments to another function, with the expectation that the callback function will be executed at a later time. Callbacks are often used in asynchronous programming, where we want to execute a certain action only after a previous action has completed.&lt;br&gt;
Let me explain this concept with two code examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doSomething(callback) {
  console.log("Do something");
  callback();
}

function doSomethingElse() {
  console.log("Do something else");
}

doSomething(doSomethingElse);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare a function named &lt;em&gt;doSomething&lt;/em&gt; that accepts a callback function as an argument.&lt;/li&gt;
&lt;li&gt;Inside the function, log a message to the console to indicate that &lt;em&gt;doSomething&lt;/em&gt; is being executed.&lt;/li&gt;
&lt;li&gt;Call the callback function that was passed as an argument.&lt;/li&gt;
&lt;li&gt;Declare a function named &lt;em&gt;doSomethingElse&lt;/em&gt; that logs a message to the console.&lt;/li&gt;
&lt;li&gt;Call the &lt;em&gt;doSomething&lt;/em&gt; function, passing &lt;em&gt;doSomethingElse&lt;/em&gt; as the callback function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(function() {
  console.log("Timeout function executed");
}, 1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the &lt;em&gt;setTimeout&lt;/em&gt; function to schedule a function to be executed after a certain amount of time has passed.&lt;/li&gt;
&lt;li&gt;The first argument to &lt;em&gt;setTimeout&lt;/em&gt; is an anonymous function that logs a message to the console.&lt;/li&gt;
&lt;li&gt;The second argument is the delay time in milliseconds (1000ms = 1 second).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So there you have it! We've gone over five of the most common JavaScript interview questions and provided you with their answers. By understanding these concepts and practicing them on your own, you can boost your chances of success in your JavaScript job interviews. And don't forget to practice writing code and clearly explaining your thought process during the interview. Best of luck to you!&lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
  </channel>
</rss>
