Advance JavaScript Concepts

Advance JavaScript Concepts

Featuring Code execution cycle, Closures and DOM in JS

Closures

In JavaScript, a closure is a function that has access to variables in its outer lexical environment (an environment inside global environment) , even after that outer function has returned. In other words, a closure is created when a function is defined inside another function and has access to its parent's variables and parameters.

function outerFunction() {
  var outerVariable = 'Find me :)';

  function innerFunction() {
    console.log(outerVariable);
  }//line by line excecution

  return innerFunction;
}

var innerFunc = outerFunction();
innerFunc(); // Output: "I am outside!"

In this example, outerFunction() creates a variable outerVariable and a nested function innerFunction(). innerFunction() has access to the outerVariable even after outerFunction() has returned. When we call innerFunc(), it logs the value of outerVariable to the console.

Closures are often used to create private variables and functions in JavaScript. Because a closure has access to its parent's variables and functions, it can access and modify them even after its parent function has returned. This can be useful for encapsulating data and preventing it from being accessed or modified from outside the closure.

Code execution cycle

The code execution cycle in JavaScript can be divided into two main phases:

  1. Creation Phase

  2. Execution Phase

Creation Phase:

During the creation phase, the following tasks are performed:

  1. Variable and Function Declarations: In this phase, JavaScript scans through the code and creates all variable and function declarations, known as hoisting. This means that variables and functions can be used before they are declared in the code.

  2. Scope Chain and 'this' Binding: During this phase, JavaScript sets up the scope chain and determines the value of 'this' for each function.

  3. Creation of the 'Global Object' and 'this' Keyword: The global object is created in the creation phase, and 'this' keyword is set to the global object by default.

  4. Setting up Event Listeners: If there are any event listeners in the code, they are set up during this phase.

Execution Phase:

During the execution phase, the code is executed line by line. In this phase, the following tasks are performed:

  1. Code Execution: The code is executed line by line, starting from the top of the file.

  2. Function Calls and Execution: When a function is called, a new execution context is created and added to the call stack. The code inside the function is executed, and any variables declared inside the function are added to the function's scope.

  3. Variable Assignments: During the execution phase, variable assignments are made and values are stored in memory.

  4. Event Handling: If there are any events in the code, they are handled during the execution phase.

  5. Garbage Collection: When the code has finished executing, any variables or objects that are no longer needed are removed from memory.

Understanding the code execution cycle in JavaScript is important for writing efficient and error-free code.

DOM (Document object model) in JS

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects, so that programming languages can interact with the page.

DOM manipulation refers to the process of changing the structure, style, or content of a web page using JavaScript. DOM manipulation is a powerful feature of JavaScript, as it allows developers to dynamically update the content of a web page in response to user interaction, data changes, or other events.

Some common DOM manipulation techniques include:

  1. Selecting Elements: DOM manipulation starts with selecting one or more elements on the page using JavaScript. This is typically done using the document.getElementById(), document.getElementsByClassName(), or document.querySelector() methods.

  2. Changing Element Properties: Once an element has been selected, its properties can be changed using JavaScript. For example, the text content of an element can be changed using the textContent property, or the value of an input field can be changed using the value property.

  3. Adding and Removing Elements: DOM manipulation also involves adding new elements to the page or removing existing ones. This can be done using the createElement(), appendChild(), and removeChild() methods.

  4. Modifying Styles: CSS styles can also be modified using JavaScript. This can be done using the style property of an element, which allows developers to modify the element's CSS properties such as color, font-size, or background-color.

    Example for dom manipulation :

<!-- HTML code -->
<div id="myDiv">Hello, world!</div>
// JavaScript code
var myDiv = document.getElementById('myDiv');
myDiv.textContent = 'Hello, JavaScript!'; // Change the text content of the div
myDiv.style.color = 'red'; // Change the text color to red

In this example, we first select the div element with the ID myDiv using the getElementById() method. We then use the textContent and style properties to change the text content and color of the div element.

DOM manipulation is a powerful feature of JavaScript that allows developers to create dynamic and interactive web pages.