Tag: javascript

63 votes
30 answers
1K 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": "v

avatar
Santiago
0 1 0 9
asked 07 September, 2021
51 votes
25 answers
3.1K 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(&ap

avatar
Solomon
0 1 0 9
asked 07 September, 2021
54 votes
25 answers
1.6K 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

avatar
Bryan
0 1 0 9
asked 07 September, 2021
71 votes
30 answers
2.9K 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: 1

avatar
Lashawna
0 1 0 9
asked 07 September, 2021
52 votes
30 answers
1.1K 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 avail

avatar
Elicia
0 1 0 9
asked 07 September, 2021
60 votes
30 answers
598 views
Sort array of objects by string property value

I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jam

avatar
Daniel
0 1 0 9
asked 07 September, 2021
53 votes
30 answers
1.5K 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 t

avatar
Hallie
0 1 0 9
asked 07 September, 2021
59 votes
25 answers
3K 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

avatar
Yolando
0 1 0 9
asked 07 September, 2021
56 votes
20 answers
1.7K 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 jQu

avatar
Arline
0 1 0 9
asked 07 September, 2021
58 votes
30 answers
409 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",

avatar
Charis
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.9K 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, l

avatar
Adolfo
0 1 0 9
asked 07 September, 2021
59 votes
30 answers
2.4K 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(tex

avatar
Yolando
0 1 0 9
asked 07 September, 2021
65 votes
30 answers
1.1K 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 che

avatar
Nereida
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.2K 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 i

avatar
Lynette
0 1 0 9
asked 07 September, 2021
59 votes
8 answers
1.8K 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 whil

avatar
Shantay
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
2.5K 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

avatar
Jefferey
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.8K 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

avatar
Wilfredo
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
459 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.

avatar
Arline
0 1 0 9
asked 07 September, 2021
48 votes
30 answers
2K 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 onl

avatar
Omar
0 1 0 9
asked 07 September, 2021
61 votes
30 answers
1.7K views
How to create a GUID / UUID

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are avai

avatar
Lynette
0 1 0 9
asked 07 September, 2021