Sunday, 26 February 2023

Tabular comparison of the differences between the var, let, and const keywords in JavaScrip

 Here is a tabular comparison of the differences between the var, let, and const keywords in JavaScript:

Video Lecture At: https://youtu.be/2SvlP9HVsGc

KeywordScopeHoistingReassignment
varFunction-scopedYesAllowed
letBlock-scopedNoAllowed
constBlock-scopedNoNot allowed

Scope: The scope of a variable determines where it can be accessed in the program. Variables declared with var are function-scoped, which means they can be accessed anywhere within the function they are declared in. Variables declared with let and const are block-scoped, which means they can only be accessed within the block of code in which they are defined.

Hoisting: Hoisting is a JavaScript behavior where variable declarations are moved to the top of their scope before the code is executed. Variables declared with var are hoisted, which means they can be accessed before they are declared. This can sometimes lead to unexpected behavior. Variables declared with let and const are not hoisted.

Reassignment: Reassignment refers to the ability to change the value of a variable after it has been declared. Variables declared with var and let can be reassigned, while variables declared with const cannot be reassigned.

Understanding these differences is important for writing clean and effective JavaScript code. Using the appropriate keyword for each situation can help prevent bugs and improve the overall readability and maintainability of the code.





JavaScript: var, let, and const Variable

Urdu Video At: https://youtu.be/2SvlP9HVsGc

In JavaScript, variables are used to store data that can be accessed and manipulated throughout a program. There are three keywords that can be used to declare variables in JavaScript: var, let, and const. You can also not use any variable keyword. Each of these keywords has its own unique characteristics and use cases.

The var keyword was the original way to declare variables in JavaScript, and it is still used in some legacy code. However, its use has been largely replaced by the let and const keywords.

The let keyword was introduced in ECMAScript 6 and is used to declare block-scoped variables. This means that variables declared with let are only accessible within the block of code in which they are defined. For example:


javascript

function exampleFunction() {

  let x = 1;

  if (true) {

    let x = 2;

    console.log(x); // Output: 2

  }

  console.log(x); // Output: 1

}

In this example, the variable x is declared with let inside the if statement. This creates a new variable that is only accessible within the if block. The value of x outside the if block is still 1.The const keyword is also introduced in ECMAScript 6 and is used to declare constants. A constant is a variable that cannot be reassigned once it has been declared. This makes it useful for values that should not change throughout a program, such as mathematical constants or configuration values. For example:

const PI = 3.14159;

PI = 3; // Throws an error: "Assignment to constant variable."

In this example, the value of PI is declared as a constant using the const keyword. Any attempt to reassign the value of PI will result in an error.

In summary, the var keyword is the original way to declare variables in JavaScript, but its use has been largely replaced by the let and const keywords. The let keyword is used to declare block-scoped variables, while the const keyword is used to declare constants that cannot be reassigned. Understanding the differences between these keywords is important for writing clean and effective JavaScript code.




Java Script Basics with Turn Light On and Off Example

 What is Java Script? 

JavaScript is a popular programming language that is widely used to create dynamic and interactive websites. It was first introduced in 1995 by Brendan Eich, a software engineer at Netscape, and has since become one of the most widely used languages on the web.

JavaScript is a high-level programming language that is used to add interactivity and functionality to web pages. It is often used in conjunction with HTML and CSS to create dynamic web pages that can respond to user input and change in real-time. It is a versatile language that can be used to create everything from simple animations to complex web applications.

One of the key features of JavaScript is its ability to manipulate the Document Object Model (DOM). The DOM is a programming interface that represents the structure of an HTML or XML document as a tree-like structure. By using JavaScript to manipulate the DOM, developers can create dynamic web pages that respond to user input and change in real-time.

JavaScript is also an object-oriented language, which means that it uses objects to represent data and behavior. This allows developers to create complex data structures and reuse code, making it easier to write and maintain complex applications.

Another key feature of JavaScript is its support for asynchronous programming. Asynchronous programming allows developers to write code that can run in the background, without blocking the main thread of execution. This is especially useful when working with web applications that need to make requests to a server or perform other time-consuming tasks.

One of the biggest advantages of JavaScript is its portability. Because it is a client-side language, it can run on almost any device with a web browser. This makes it a great choice for creating web applications that can be accessed from a variety of devices and platforms.

JavaScript is also an open-source language, which means that anyone can contribute to its development and improvement. This has led to a large and active community of developers who share their knowledge and resources to help others learn and improve their skills.

While JavaScript has many advantages, it is not without its challenges. One of the biggest challenges is the fact that it is an interpreted language, which means that it is not as fast as compiled languages like C++ or Java. This can make it more challenging to create high-performance applications.

Another challenge is the fact that JavaScript code can be difficult to debug. Because it is a dynamically typed language, it can be challenging to catch errors before they cause problems in the application.

Despite these challenges, JavaScript remains one of the most widely used languages on the web. Its versatility, portability, and ease of use make it a great choice for creating dynamic and interactive web applications. Whether you are a beginner or an experienced developer, learning JavaScript is a valuable skill that can help you create powerful and engaging web experiences.


Example 

<!DOCTYPE html>

<html>

<body>

<h2>What Can JavaScript Do?</h2>

<p>JavaScript can change HTML attribute values.</p>

<p>In this case JavaScript changes the value of the src (source) attribute of an image.</p>

<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">

Turn on the light

</button>

<img id="myImage" src="pic_bulboff.gif" style="width:100px">

<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>

</body>

</html>







Refference 
https://www.w3schools.com/js/js_intro.asp


Friday, 24 February 2023

Web What is Promise Object ? Fetch VS Axios

 


 

Promise

 

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

 

 

Axios is a promise-based HTTP library that lets developers make requests to either their own or a third-party server to fetch data. It offers different ways of making requests such as GET, POST, PUT/PATCH, and DELETE.

 

Axios works by making HTTP requests with NodeJS and XMLHttpRequests on the browser. If the request was successful, you will receive a response with the data requested. If the request failed, you will get an error. You can also intercept the requests and responses and transform or modify them


 

 Axios

1)Axios is a third-party HTTP client library for making network requests.

 

2) It is is a promise-based HTTP client.

 

3)  Axios can be installed by using a CDN or package manager

 

       CDN: <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

                             Or Install using NPM - npm install axios Or Install using Yarn - yarn add axios

4) Code:    axios.get('https://codilime.com/') .then(response => console.log(response.data));

 

5) By Default Stringify

Fetch

 

1) Fetch is a built-in API

 

2) Fetch, like Axios, is a promise-based HTTP client.

 

3) we don't have to install or import anything. It's available in all modern browsers,

 

 4) Code:    fetch('https://codilime.com/') .then(response => response.json()) .then(console.log);

 

5) Explicit Stringify

 

 

Code

Axios

axios(url, {

  // configuration options

})

 

 

axios(url, {
  method: 'post',
  timeout: 1000,
  headers: {
    "Content-Type": "application/json",
  },
  data: {
    property: "value",
  },
})

 

Fetch

fetch(url, {
  method: 'POST',
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    property: "value",
  }),
})

 

 

Reference: https://codilime.com/blog/axios-vs-fetch/#:~:text=Different%20properties%20are%20used%20for,API%20using%20the%20POST%20method

Kleene’s theorem with Proof

 

Video Lectures

Part1: https://youtu.be/MVrPfSgMxO8 

Part 2:    https://youtu.be/v296i02bZ6Q

Thus, to prove Kleene’s theorem, we need to prove 3 parts:

Part 1: Every language that can be defined by a finite automaton can also be defined by a transition graph.

Part 2: Every language that can be defined by a transition graph can also be defined by a regular expression.

Part 3: Every language that can be defined by a regular expression can also be defined by a finite automaton.


Proof of Part 1

 we know that every finite automaton is itself already a transition graph. Therefore, any language that has been defined by a finite automaton has already been defined by a transition graph.

Proof of Part 2: Turning TGs into Regular Expressions
We prove this part by providing a constructive algorithm:

– We present an algorithm that starts out with a transition graph and ends up with a regular expression that defines the same language.
– To be acceptable as a method of proof, the algorithm we present will satisfy two criteria: 
It works for every conceivable TG, and 
It guarantees to finish its job in a finite number of steps.


Algorithm 
Step 1: Create a unique, unenterable minus state and a unique, unleaveable plus state.

Step 2: One by one, in any order, bypass and eliminate all the non-minus or non-plus states in the TG. A state is bypassed by connecting each incoming edge with each outgoing edge. The label of each resultant edge is the concatenation of the label on the incoming edge with the label on the loop edge (if there is one) and the label on the outgoing edge.

Step 3: When two states are joined by more than one edge going in the same direction, unify them by adding their labels.

Step 4: Finally, when all that is left is one edge from - to +, the label on that edge is a regular expression that generates the same language as was recognized by the original TG.