You only need one temporary variable.
var b = list[y]; list[y] = list[x]; list[x] = b;
Edit hijacking top answer 10 years later with a lot of ES6 adoption under our belts:
Given the array arr = [1,2,3,4]
, you can swap values in one line now like so:
[arr[0], arr[1]] = [arr[1], arr[0]];
This would produce the array [2,1,3,4]
. This is destructuring assignment.
Related Posts:
- What does “javascript:void(0)” mean?
- For-each over an array in JavaScript
- How can I remove a specific item from an array?
- require is not defined? Node.js
- What is NODE_ENV and how to use it in Express?
- function updateMap for Google Maps API
- Loop inside React JSX
- Encode URL in JavaScript?
- JavaScript null check
- JavaScript “Uncaught TypeError: object is not a function” associativity question
- Module not found can’t resolve
- How can I get file extensions with JavaScript?
- How to detect Safari, Chrome, IE, Firefox and Opera browser?
- What does on() in JavaScript do?
- AngularJS : Factory and Service?
- What’s the difference between console.dir and console.log?
- Use images like checkboxes
- What is the difference between document.location.href and document.location?
- Javascript Uncaught TypeError : .split is not a function
- Uncaught TypeError: Cannot read property ‘msie’ of undefined
- How to hide a div with jQuery?v
- How do I pass variables and data from PHP to JavaScript?
- How do I set/unset a cookie with jQuery?
- Calling onclick on a radiobutton list using javascript
- How to properly export an ES6 class in Node 4?
- Uncaught Typeerror: cannot read property ‘innerHTML’ of null
- Sorting HTML table with JavaScript
- Relation between CommonJS, AMD and RequireJS?
- How to loop through an array containing objects and access their properties
- DOMException: Failed to load because no supported source was found
- Counting the occurrences / frequency of array elements
- how to download file in react js
- TypeError: $(…).DataTable is not a function
- Trying to use fetch and pass in mode: no-cors
- Moment.js transform to date object
- jQuery explode string like PHP
- Call Javascript function from URL/address bar
- Anagrams finder in javascript
- Validate date in dd/mm/yyyy format using JQuery Validate
- Setting and getting localStorage with jQuery
- How to subtract days from a plain Date?
- Stopping a JavaScript function when a certain condition is met
- Showing an image from an array of images – Javascript
- Javascript isnull
- How do you completely remove Ionic and Cordova installation from mac?
- What is the best way to detect a mobile device?
- How to filter JSON Data in JavaScript or jQuery?
- What’s the best way to reload / refresh an iframe?
- How do I check if a cookie exists?
- Get querystring from URL using jQuery [duplicate]
- Check if string contains only digits
- JS: iterating over result of getElementsByClassName using Array.forEach
- jQuery checkbox change and click event
- Error: listen EACCES 0.0.0.0:80 OSx Node.js
- jQuery selectors on custom data attributes using HTML5
- How to check if a value is not null and not empty string in JS
- How do I refresh a DIV content?
- TypeError: abc.getAttribute is not a function
- what is ScriptResource.axd in the page load in web app?
- Update style of a component onScroll in React.js
- Fire oninput event with jQuery
- javascript includes() case insensitive
- Reading local text file into a JavaScript array
- “NS_ERROR_DOM_BAD_URI: Access to restricted URI denied”
- How can I unset a JavaScript variable?
- D3.js: what is ‘g’ in .append(“g”) D3.js code?
- Correct modification of state arrays in React.js
- Angular 2 Sort table columns
- JavaScript string encryption and decryption?
- Reset Undo on the tinymce editor
- Combine enqueue js without affecting dependencies
- WP Customizer set a value via javascript (to create presets)
- JavaScript in page doesn’t open new window [closed]
- preserve url parameters from affliliate sites
- wp_enqueue_script adds only the first script
- Add javascript to a woocommerce page template
- Dynamically insert values in a WordPress SelectControl Options field
- Vimeo froogaloop
- What javascript libs & frameworks are integrated in WordPress vanilla?
- Conditional fields in contact form 7 not working
- can a buttongroup have a label?
- Preloader for a WordPress Site
- Trying to integrate Zoho Campaign Form [closed]
- Enqueueing a script and a style sheet not working
- InnerBlocks restricting does not work on nested blocks
- Uppload image from another source wp.media
- Use onfocus event in Contact Form 7
- How to open the add media dialogue it in a certain state / tab?
- Removing auto versioning of JS and loading to header
- enqueue script won’t work (enqueue style does work)
- Putting custom html/js page into Elementor as it’s own block
- How to get file object from file_attachment?
- Drag-to-Scroll Landingpage
- Easiest way to find JS conflicts
- Nothing in dashboard that uses javascript works
- XMLHttpRequest of admin data to public
- How to execute WP shortcode in a javascript string “”
- Loaded JavaScript file not showing [duplicate]
- PHP or JS for header image rotator?
- Replacing & in JS code on pages