Aws
Auth
Axios
Admin
Angular
Android
Atom Payment
BPO
BcryptJs
Bootstrap
Basic Computer
C Language
C++
Css
Canva
Common questions
CorelDraw
Cloudinary
Content Writer
DSA
Django
Error
Excel
ExpressJs
Flutter
Github
Graphql
GoDaddy
HR
Html5
Hostinger
Jwt
Java
Json
Jquery
Javascript
Linux OS
Loopback API
MySQL
Manager
MongoDB
Marketing
MS Office
Mongoose
NodeJs
NextJs
Php
Python
Photoshop
PostgreSQL
PayU Payment
Paypal Payment
Redux
ReactJs
Router
React Native
React Router Dom
React Helmet
Sass
SEO
SMO
Stripe Payment
System Administrator
Software Testing
Typescript
Tailwind
Telesales
Tally
VueJs
Windows OS
XML
Description : Closures are a fundamental concept in JavaScript. Explain how they work and provide an example.
A closure in JavaScript is a function that has access to its own scope, the scope of the outer function, and the global scope. It allows a function to retain access to variables from its containing scope even after that function has finished executing. For example, a function defined inside another function can access the outer function's variables. A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). When you create a closure, you gain access to an outer function’s scope from an inner function. Closures are automatically created every time a function is defined in JavaScript. function foo() { let b = 1; function inner() { return b; } return inner; } let get_func_inner = foo(); console.log(get_func_inner()); console.log(get_func_inner()); console.log(get_func_inner()); <!DOCTYPE html> <html> <body> <h2>JavaScript Closures</h2> <button type="button" onclick="initaccount()"> Click Me! </button> <p id="demo"></p> <script> function initaccount() { var balance = 1000; function currentbalance() { balance = balance - 100; alert(balance); } currentbalance(); } </script> </body> </html>
Category : Javascript
Created Date : 9/6/2024
What is the `String.prototype.anchor` method in JavaScript?
`String.prototype.anchor` creates an HTML `<a>` element wrapping the string with a specified name attribute. This method is deprecated and should not be used in modern applications. const str = 'Click here'; const anchoredStr = str.anchor('top'); console.log(anchoredStr); // '<a name="top">Click here</a>'
`String.prototype.anchor` creates an HTML `<a>` element wrapping the string with a specified name attribute. This method is deprecated and should not be used in modern applications. const str = 'Click here'; const anchoredStr = str.anchor('top'); console.log(anchoredStr); // '<a name="top">Click here</a>'
What is the `String.prototype.small` method in JavaScript?
`String.prototype.small` returns a string wrapped in HTML `<small>` tags. This method is deprecated and should not be used in modern applications. const str = 'hello'; const smallStr = str.small(); console.log(smallStr); // '<small>hello</small>'
`String.prototype.small` returns a string wrapped in HTML `<small>` tags. This method is deprecated and should not be used in modern applications. const str = 'hello'; const smallStr = str.small(); console.log(smallStr); // '<small>hello</small>'