Sitelet https://web.archive.org/web/20220408192110/https://stackoverflow.com/questions/tagged/javascript

Questions tagged [javascript]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

Filter by
Sorted by
Tagged with
10478 votes
127 answers
9.8m views

How can I remove a specific item from an array?

How do I remove a specific value from an array? Something like: array.remove(value); // removes all elements with value I have to use core JavaScript. Frameworks are not allowed.
user avatar
  • 116k
8344 votes
63 answers
3.0m views

How do I check if an element is hidden in jQuery?

Is it possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle()? How would you test if an element is visible or hidden?
user avatar
8155 votes
29 answers
1.2m views

What does "use strict" do in JavaScript, and what is the reasoning behind it?

Recently, I ran some of my JavaScript code through Crockford's JSLint, and it gave the following error: Problem at line 1 character 1: Missing "use strict" statement. Doing some searching, I ...
user avatar
  • 93.4k
7710 votes
58 answers
7.2m views

How do I redirect to another webpage?

How can I redirect the user from one page to another using jQuery or pure JavaScript?
user avatar
7625 votes
86 answers
1.6m views

How do JavaScript closures work?

How would you explain JavaScript closures to someone with a knowledge of the concepts they consist of (for example functions, variables and the like), but does not understand closures themselves? I ...
7417 votes
3 answers
7.1m views

How to check whether a string contains a substring in JavaScript?

Usually I would expect a String.contains() method, but there doesn't seem to be one. What is a reasonable way to check for this?
7388 votes
41 answers
1.1m views

var functionName = function() {} vs function functionName() {}

I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent. The previous developer used two ...
user avatar
6964 votes
33 answers
2.8m views

How do I remove a property from a JavaScript object?

Say I create an object as follows: let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; How should I ...
user avatar
  • 91.2k
6329 votes
43 answers
1.9m views

How to return the response from an asynchronous call

I have a function foo which makes an asynchronous request. How can I return the response/result from foo? I am trying to return the value from the callback, as well as assigning the result to a local ...
user avatar
5928 votes
68 answers
3.9m views

How do I include a JavaScript file in another JavaScript file?

Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?
user avatar
  • 89.5k
5685 votes
37 answers
1.9m views

What's the difference between using "let" and "var"?

ECMAScript 6 introduced the let statement. I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently than the var keyword. What are the differences? ...
user avatar
  • 102k
5656 votes
48 answers
2.0m views

Which equals operator (== vs ===) should be used in JavaScript comparisons?

I'm using JSLint to go through JavaScript, and it's returning many suggestions to replace == (two equals signs) with === (three equals signs) when doing things like comparing idSele_UNVEHtype.value....
user avatar
  • 57.4k
5319 votes
40 answers
4.9m views

For-each over an array in JavaScript

How can I loop through all the entries in an array using JavaScript?
user avatar
  • 55.2k
5256 votes
103 answers
4.0m views

What's the best way to validate an email address in JavaScript?

What's the best way to validate an email address in JavaScript with a regular expression?
5173 votes
67 answers
2.5m views

What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(...
5127 votes
79 answers
4.1m views

How to replace all occurrences of a string in JavaScript

I have this string in my JavaScript code: "Test abc test test abc test test test abc test test abc" Doing: str = str.replace('abc', ''); Seems to only remove the first occurrence of abc in ...
user avatar
  • 248k
4989 votes
66 answers
4.5m views

How do I check whether a checkbox is checked in jQuery?

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery. For example, if the age checkbox is checked, then I need to show a textbox to enter ...
4876 votes
69 answers
2.5m views

How to create a GUID / UUID

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, ...
4593 votes
56 answers
3.1m views

How do I check if an array includes a value in JavaScript?

What is the most concise and efficient way to find out if a JavaScript array contains a value? This is the only way I know to do it: function contains(a, obj) { for (var i = 0; i < a.length; i++...
user avatar
  • 70.5k
4574 votes
117 answers
3.0m views

How do I make the first letter of a string uppercase in JavaScript?

How do I make the first letter of a string uppercase, but not change the case of any of the other letters? For example: "this is a test" → "This is a test" "the Eiffel Tower&...
user avatar
  • 46.4k
4510 votes
15 answers
847k views

"Thinking in AngularJS" if I have a jQuery background? [closed]

Suppose I'm familiar with developing client-side applications in jQuery, but now I'd like to start using AngularJS. Can you describe the paradigm shift that is necessary? Here are a few questions that ...
user avatar
4487 votes
41 answers
3.5m views

How do you get a timestamp in JavaScript?

Something similar to Unix's timestamp, that is a single number that represents the current time and date. Either as a number or a string.
user avatar
  • 265k
4436 votes
43 answers
3.6m views

Setting "checked" for a checkbox with jQuery

I'd like to do something like this to tick a checkbox using jQuery: $(".myCheckBox").checked(true); or $(".myCheckBox").selected(true); Does such a thing exist?
user avatar
  • 54.6k
4333 votes
8 answers
570k views

Why does Google prepend while(1); to their JSON responses?

Why does Google prepend while(1); to their (private) JSON responses? For example, here's a response while turning a calendar on and off in Google Calendar: while (1); [ ['u', [ ['smsSentFlag', ...
user avatar
  • 40.9k
4319 votes
55 answers
2.4m views

Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.? ...
4079 votes
68 answers
2.5m views

How do I copy to the clipboard in JavaScript?

What is the best way to copy text to the clipboard (multi-browser)? I have tried: function copyToClipboard(text) { if (window.clipboardData) { // Internet Explorer window.clipboardData....
user avatar
3898 votes
49 answers
3.5m views

How do I test for an empty JavaScript object?

After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case?
user avatar
  • 39.4k
3809 votes
39 answers
774k views

What is the !! (not not) operator in JavaScript?

I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does? The context in which I saw ...
user avatar
3750 votes
25 answers
2.8m views

How to insert an item into an array at a specific index (JavaScript)

I am looking for a JavaScript array insert method, in the style of: arr.insert(index, item) Preferably in jQuery, but any JavaScript implementation will do at this point.
user avatar
  • 76.1k
3696 votes
44 answers
4.6m views

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can you do the same in ...
user avatar
3683 votes
29 answers
2.9m views

Checking if a key exists in a JavaScript object?

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
user avatar
  • 49.1k
3608 votes
55 answers
2.4m views

Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: '...
user avatar
3586 votes
88 answers
3.5m views

How to round to at most 2 decimal places, if necessary?

I'd like to round at most 2 decimal places, but only if necessary. Input: 10 1.7777777 9.1 Output: 10 1.78 9.1 How can I do this in JavaScript?
user avatar
3558 votes
59 answers
4.4m views

How can I check for an empty/undefined/null string in JavaScript?

I saw this question, but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?
user avatar
  • 63.7k
3479 votes
76 answers
2.2m views

How do I correctly clone a JavaScript object?

I have an object x. I'd like to copy it as object y, such that changes to y do not modify x. I realized that copying objects derived from built-in JavaScript objects will result in extra, unwanted ...
user avatar
3446 votes
26 answers
3.3m views

Get the current URL with JavaScript?

All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a variable to do with as I ...
user avatar
3352 votes
44 answers
2.5m views

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; Now I want to loop through all p elements (p1, p2, p3...) And get their keys ...
user avatar
  • 41.7k
3323 votes
25 answers
771k views

What is the difference between call and apply?

What is the difference between using Function.prototype.apply() and Function.prototype.call() to invoke a function? var func = function() { alert('hello!'); }; func.apply(); vs func.call(); Are ...
user avatar
  • 36.7k
3204 votes
51 answers
1.9m views

How can I check if an object is an array? [duplicate]

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item so I can loop over it without ...
user avatar
  • 253k
3168 votes
34 answers
3.1m views

How can I change an element's class with JavaScript?

How can I change the class of an HTML element in response to an onclick or any other events using JavaScript?
user avatar
  • 34.7k
3158 votes
14 answers
905k views

event.preventDefault() vs. return false

When I want to prevent other event handlers from executing after a certain event is fired, I can use one of two techniques. I'll use jQuery in the examples, but this applies to plain-JS as well: 1. ...
user avatar
  • 67.8k
3149 votes
88 answers
3.8m views

What is the JavaScript version of sleep()?

Is there a better way to engineer a sleep in JavaScript than the following pausecomp function (taken from here)? function pausecomp(millis) { var date = new Date(); var curDate = null; do ...
3140 votes
98 answers
2.8m views

How can I convert a string to boolean in JavaScript?

Can I convert a string representing a boolean value (e.g., 'true', 'false') into a intrinsic type in JavaScript? I have a hidden form in HTML that is updated based upon a user's selection within a ...
3135 votes
43 answers
2.0m views

Creating multiline strings in JavaScript

I have the following code in Ruby. I want to convert this code into JavaScript. What is the equivalent code in JS? text = <<"HERE" This Is A Multiline String HERE
user avatar
  • 36.2k
3101 votes
50 answers
1.4m views

Detecting an undefined object property

What's the best way of checking if an object property in JavaScript is undefined?
user avatar
3100 votes
44 answers
446k views

JavaScript closure inside loops – simple practical example

var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value: " + i); ...
user avatar
  • 517k
3079 votes
34 answers
1.4m views

How can I upload files asynchronously with jQuery?

I would like to upload a file asynchronously with jQuery. $(document).ready(function () { $("#uploadbutton").click(function () { var filename = $("#file").val(); $.ajax({...
user avatar
3045 votes
20 answers
280k views

Is it possible to apply CSS to half of a character?

What I am looking for: A way to style one HALF of a character. (In this case, half the letter being transparent) What I have currently searched for and tried (With no luck): Methods for styling ...
user avatar
3028 votes
62 answers
5.4m views

How to format a JavaScript date

In JavaScript, how can I format a date object to print as 10-Aug-2010?
user avatar
  • 176k
3018 votes
29 answers
1.4m views

pretty-print JSON using JavaScript

How can I display JSON in an easy-to-read (for human readers) format? I'm looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc.
user avatar
  • 61.8k

1
2 3 4 5
…
47183