Scoping rules
The main difference is scoping rules. Variables declared by var
keyword are scoped to the immediate function body (hence the function scope) while let
variables are scoped to the immediate enclosing block denoted by { }
(hence the block scope).
function run() { var foo = "Foo"; let bar = "Bar"; console.log(foo, bar); // Foo Bar { var moo = "Mooo" let baz = "Bazz"; console.log(moo, baz); // Mooo Bazz } console.log(moo); // Mooo console.log(baz); // ReferenceError } run();
Related Posts:
- “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6
- Define a global variable in a JavaScript function
- How to fix “Cannot read property ‘addEventListener’ of null” error [duplicate]
- How to import jquery using ES6 syntax?
- Node.js – SyntaxError: Unexpected token import
- Uncaught TypeError: Cannot read property ‘length’ of undefined
- Node.js – SyntaxError: Unexpected token import
- forEach is not a function error with JavaScript array
- Getting Unexpected Token Export
- React with ES7: Uncaught TypeError: Cannot read property ‘state’ of undefined
- Using Node.js require vs. ES6 import/export
- Cannot find module ‘@angular-devkit/core’
- Why Is `Export Default Const` invalid?
- How can I mock an ES6 module import using Jest?
- How can I quickly retrieve a value from a MapIterator returned from Object.keys function?
- How to scroll to an element?
- ES6: Create Strings using Template Literals – Freecodecamp
- Cannot read property ‘bind’ of undefined. React.js
- How to add multiple classes to a ReactJS Component?
- SyntaxError: ‘import’ and ‘export’ may appear only with ‘sourceType: module’ – Gulp
- Angular 2 – Using ‘this’ inside setTimeout
- This.props.dispatch not a function – React-Redux
- How do I pass the this context to a function?
- Does ECMAScript 6 have a convention for abstract classes?
- Expected ‘this’ to be used by class method
- .filter is not a function
- where is create-react-app webpack config and files?
- clearInterval() not working [duplicate]
- Getting “Cannot call a class as a function” in my React Project
- Error: Couldn’t find preset “es2015” relative to directory “/Users/username”
- Is there a way to create interfaces in ES6 / Node 4?
- Getting a UnhandledPromiseRejectionWarning when testing using mocha/chai
- Creating multiline strings in JavaScript
- ActiveX – Automation Server Can’t Create Object
- How to loop through an array containing objects and access their properties
- Remove all child elements of a DOM node in JavaScript
- Why is process.env.NODE_ENV undefined?
- Better way to sum a property value in an array
- Javascript require() function giving ReferenceError: require is not defined
- jQuery AJAX cross domain
- TypeError: Cannot read property ‘get’ of undefined on AngularJS
- How to remove text from a string?
- Convert character to ASCII code in JavaScript
- Disable same origin policy in Chrome
- How do I remove a key from a JavaScript object?
- What is causing the error `string.split is not a function`?
- How do I check whether an array contains a string in TypeScript?
- alert a variable value
- How to check a radio button with jQuery?
- Get current URL with jQuery?
- How to get JSON from URL in JavaScript?
- How do you put an image file in a json object?
- How can I add a class to a DOM element in JavaScript?
- Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Transition which is inside StrictMode
- Module not found: Error: Can’t resolve ‘fs’ in
- TypeError: $(…).modal is not a function with bootstrap Modal
- How to do fade-in and fade-out with JavaScript and CSS
- How to close current tab in a browser window?
- npm WARN enoent ENOENT: no such file or directory, open ‘C:\Users\Nuwanst\package.json’
- How to change value of object which is inside an array using JavaScript or jQuery?
- How to detect pressing Enter on keyboard using jQuery?
- How to make your Discord Bot always active
- How to make an HTML back link?
- How to fix getImageData() error The canvas has been tainted by cross-origin data?
- How to make fadeOut effect with pure JavaScript
- How to enable a disabled checkbox dynamically?
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- What is ‘xmlhttp.setRequestHeader();’ and in which situations is it used?
- Write / add data in JSON file using Node.js
- What does wp-embed.min.js do in WordPress 4.4?
- Gutenberg – remove / add blocks with custom script
- Gutenberg extend blocks add new class name
- How do I add custom attributes to javascript tags in WordPress?
- Rest API authentication issue when called from fetch request in bundle.js
- Strange gibberish JavaScript in Editor – site hacked?
- wp_enqueue_script & constants?
- Resolving Javascript errors likely related to Gantry framework
- WordPress customizer get control
- How to load JS for specific screen size using wp_enqueue_script?
- I am trying to add current logged in user to my zoho chat
- Where to upload JavaScript file in WordPress
- Conditional save return on Gutenberg Block
- Can I alter the block editor’s paste text behavior?
- Custom Block – save.js function not saving attributes
- Headless wp with react. How to handle routes?
- Where to paste Google Map Snippet / JavaScript / CSS for WordPress integration
- Issues trying to get an element by id
- Dynamically add Js
- Change background video when scrolling reaches a certain ID
- Contact Form 7 Conditional redirect
- Dynamically changing logo on scroll function [closed]
- Remove WordPress default registration client side validation?
- How could I add load more posts to my theme? [closed]
- Live Time on WP showing weirdly [closed]
- Javascript button to call custom fields data
- Table block variation with header section activated
- WP REST API – “rest_user_cannot_view” ONLY on specific users
- Uncaught ReferenceError: tippy is not defined
- How to add a new attribute to core wp block editor without npm?