It’s all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of JavaScript code.
For example, as mentioned in a comment by Alexander:
(function() { var foo = 3; console.log(foo); })(); console.log(foo);
This will first log 3
and then throw an error on the next console.log
because foo
is not defined.
Related Posts:
- Python nonlocal statement
- Static variables in JavaScript
- Split string into array
- convert Hsl to rgb and hex
- “Uncaught TypeError: Cannot read property ‘style’ of null at.. “
- Difference between document.referrer and window.parent.location.href
- JavaScript replace/regex
- how to render a react component using ReactDOM Render
- Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
- OnChange event using React JS for drop down
- how to get the last character of a string?
- Set timeout for ajax (jQuery)
- Escaping Discord subset of markdown
- ESLint Parsing error: Unexpected token
- Angular ForEach in Angular4/Typescript?
- How to call a JavaScript function from PHP?
- bootstrap-datetimepicker for bootstrap 4
- Proper way to wait for one function to finish before continuing?
- XMLHttpRequest module not defined/found
- Bootstrap 4 Dropdown Menu not working?
- Hide element by class in pure Javascript
- document.getElementsByClassName().innerHTML always returns “undefined”
- Bower: ENOGIT Git is not installed or not in the PATH
- Converting an object to a string
- SyntaxError: expected expression, got ‘<'
- How to read data From *.CSV file using javascript?
- Attaching ‘onclick’ event to D3 chart background
- declaration or statement expected javascript/typescript
- The preferred way of creating a new element with jQuery
- How to pass parameters using ui-sref in ui-router to controller
- How to make fadeOut effect with pure JavaScript
- How to call a webservice method from an html page [javascript] with out refresh the page
- Create an empty object in JavaScript with {} or new Object()?
- Set scroll position
- .filter is not a function
- Function overloading in Javascript – Best practices
- Wait until flag=true
- Is it possible to reuse wp.media.editor Modal for dialogs other than media
- How to enqueue scripts on custom post add/edit pages?
- Most efficient way to add javascript file to specific post and/or pages?
- Is there a JavaScript API? How to access public and private data in JS?
- Prevent WordPress from adding linebreaks to javascript embedded in a page
- Data fetch and delete by ajax request
- Rest API authentication issue when called from fetch request in bundle.js
- How do I use WordPress PHP functions in my Javascript code?
- JavaScript, best way to use data from the loop
- Javascript not working?
- Add Item to Custom TinyMCE Menu
- Listbox return wrong value in WordPress 3.9
- Logout user and delete cookies from the client side?
- Colorbox ajax loading of outside HTML content works perfect on localhost but not on server
- Widget code is not working on WordPress.com Hosted Blogs
- wpColorPicker: How update colors on input value update?
- Simple Plugin with custom javascript wont work – no console error
- Shortcode button dosent work for all posts. Work for first post only
- Can I use custom CSS and js plugin to put JavaScript in to validate my forms
- Javascript file is not working on single.php wordpress [closed]
- Update an image block style programatically
- How to send Ajax request from my plugin in admin dashboard?
- Customizer: Multiple CSS styles on same element in Live Preview
- WordPress Media frame pre load images from ids
- Integrate OpenStreetMap on WordPress
- AJAX success response is not working but it’s saving my changes
- How do I remove the date and category form my portfolio pages
- How to return multiple arguments from AJAX callback function
- JS news ticker is not working on home page but working on other pages. How to solve this?
- Where to upload JavaScript file in WordPress
- JQuery Plugins in WordPress
- how to insert a HTML form into a javascript popup?
- get specific value of a array | PHP
- Custom Block – save.js function not saving attributes
- How do I know if I should enqueue JS code or just include it in ONE PHP function?
- How can I integrate yepnope.js with WordPress (and plugins)?
- Is it possible to isolate and use WordPress functions outside of wordpress
- Testing a Form’s Zipcode Field with Regular Expressions to Determine WordPress Page Behaviour
- Invert porfolio item order
- wp.media libary pdf type
- How to set javascript options for select2 in PHP array of plugin
- Uncaught TypeError: Cannot read properties of undefined (reading ‘message’) [closed]
- Multiple jQuery inputs or binds in Customizer Control Javascripts
- Woocommerce Checkout Page Total Price Inside Google Pay Script
- Search bar for wp menu
- Immediate (e.g. without DOMContentLoaded) JS not working
- How to make JQuery load on top of head tag before everything
- Cannot successfully execute AJAX script to call function.php specific function. Using XAMPP localhost to test
- sessionStorage saves input value in browser but it is not loaded in the form field
- Speeding up WordPress on a Local Network
- Embed Javascript code to registered users only
- How to blur the_post_thumbnail() via blur.js (jquery)?
- Script not working in post content
- Problems with plugin that fetches data from database with ajax
- How to set up php,javascript and css to allow for our finished product to be displayed within a page?
- Add jQuery function to media button
- How could I add load more posts to my theme? [closed]
- JQuery Error in theme for .live is not a function [closed]
- Javascript Loading Effect to URLs
- Pass a PHP variable (loop-audio.php) to jQuery function (js/script.js)
- Javascript 2 elements dependent onclick .toggle [closed]
- Removing ‘woocommerce-no-js’ class from body
- How to submit a button automatically after every scheduled hours?