Questions tagged [javascript]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs] etc.
2,250,034
questions
9650
votes
110answers
8.5m views
How can I remove a specific item from an array?
I have an array of numbers and I'm using the .push() method to add elements to it.
Is there a simple way to remove a specific element from an array?
I'm looking for the equivalent of something like:
...
8135
votes
60answers
2.9m 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?
7914
votes
31answers
1.1m 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 ...
7710
votes
58answers
6.8m views
How do I redirect to another webpage?
How can I redirect the user from one page to another using jQuery or pure JavaScript?
7627
votes
86answers
1.5m 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 ...
7418
votes
3answers
6.7m 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?
7224
votes
39answers
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 ...
6684
votes
31answers
2.5m 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 ...
6055
votes
42answers
1.7m 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 ...
5695
votes
64answers
3.6m 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?
5659
votes
48answers
1.9m 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....
5255
votes
39answers
1.6m 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?. ...
5175
votes
67answers
2.3m 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(...
5083
votes
40answers
4.6m views
For-each over an array in JavaScript
How can I loop through all the entries in an array using JavaScript?
I thought it was something like this:
forEach(instance in theArray)
Where theArray is my array, but this seems to be incorrect.
4935
votes
100answers
3.6m views
How to validate an email address in JavaScript
Is there a regular expression to validate an email address in JavaScript?
4880
votes
74answers
3.8m 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 ...
4863
votes
67answers
4.4m 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 ...
4624
votes
63answers
2.2m 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, ...
4510
votes
15answers
843k 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 ...
4410
votes
55answers
2.9m 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++...
4330
votes
43answers
3.5m 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?
4327
votes
40answers
3.3m 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.
4266
votes
8answers
561k 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', ...
4244
votes
106answers
2.5m 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&...
4234
votes
55answers
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.?
...
3807
votes
62answers
2.2m 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....
3805
votes
60answers
3.0m 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?
3567
votes
37answers
685k 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 ...
3508
votes
43answers
4.2m 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 ...
3453
votes
20answers
2.4m 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.
3432
votes
25answers
2.5m 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?
3357
votes
73answers
2.0m 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 ...
3310
votes
53answers
2.0m 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: '...
3300
votes
58answers
3.9m 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 ""?
3294
votes
25answers
3.1m 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 ...
3284
votes
80answers
3.0m 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?
3241
votes
25answers
746k views
What is the difference between call and apply?
What is the difference between using call and apply to invoke a function?
var func = function() {
alert('hello!');
};
func.apply(); vs func.call();
Are there performance differences between the ...
3210
votes
41answers
2.3m 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 ...
3106
votes
14answers
884k 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. ...
3073
votes
51answers
1.8m views
How to check if an object is an array?
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 ...
3036
votes
34answers
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({...
3025
votes
49answers
1.3m views
Detecting an undefined object property
What's the best way of checking if an object property in JavaScript is undefined?
3021
votes
36answers
2.9m 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?
3006
votes
44answers
422k 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);
...
2978
votes
39answers
1.8m 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
2959
votes
20answers
273k 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 ...
2948
votes
94answers
2.5m 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 ...
2934
votes
44answers
806k views
Is there an “exists” function for jQuery?
How can I check the existence of an element in jQuery?
The current code that I have is this:
if ($(selector).length > 0) {
// Do something
}
Is there a more elegant way to approach this? ...
2892
votes
30answers
4.0m views
How to append something to an array?
How do I append an object (such as a string or number) to an array in JavaScript?
2878
votes
39answers
2.3m views
How can I know which radio button is selected via jQuery?
I have two radio buttons and want to post the value of the selected one.
How can I get the value with jQuery?
I can get all of them like this:
$("form :radio")
How do I know which one is selected?