Does removing comments from JavaScript code improve performance? I realize that this is not great programing practice as comments form an intrinsic part of development. I am just interested to know if they do in fact add some overhead during compilation. Answer Whether your compiling or interpreting your JavaScript, the compiler/interpreter needs to look at the line, decide it’s a
How might I find the largest number contained in a JavaScript array?
I have a simple JavaScript Array object containing a few numbers. Is there a function that would find the largest number in this array? Answer Resig to the rescue: Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you’re not certain the array is that small.
How do I prevent a parent’s onclick event from firing when a child anchor is clicked?
I’m currently using jQuery to make a div clickable and in this div I also have anchors. The problem I’m running into is that when I click on an anchor both click events are firing (for the div and the anchor). How do I prevent the div’s onclick event from firing when an anchor is clicked? Here’s the broken code:
JavaScript moving element in the DOM
Let’s say I have three <div> elements on a page. How can I swap positions of the first and third <div>? jQuery is fine. Answer Trivial with jQuery If you want to do it repeatedly, you’ll need to use different selectors since the divs will retain their ids as they are moved around.
JavaScript equivalent of PHP’s die
Is there something like “die” in JavaScript? I’ve tried with “break”, but doesn’t work 🙂 Answer You can only break a block scope if you label it. For example: You cannot break a block scope from within a function in the scope. This means you can’t do stuff like: You can do something similar though with functions:
Sorting a JavaScript object by property name
I’ve been looking for a while and want a way to sort a Javascript object like this: and sort is alphabetically by name to get: I can’t find any code that will do this. Can anyone give me some help? Answer UPDATE from the comments: This answer is outdated. In ES6 objects keys are now ordered. See this question for
Generate random string/characters in JavaScript
I want a 5 character string composed of characters picked randomly from the set [a-zA-Z0-9]. What’s the best way to do this with JavaScript? Answer I think this will work for you:
jQuery hover(): mouseout does not fire when using overflow:auto (with scrollbars)
I’m using jQuery’s hover() helper to attach some behavior to <li> elements in a <ul> with max-height and overflow:auto CSS properties. When the height of the <ul> goes beyond max-height and the vertical scrollbar appears, hovering over the <li> elements triggers mouseOver, but if I move to the right to the scrollbar and start scrolling with the mouse, the mouseOut
Javascript to Select Multiple options [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 1 year ago. Improve this question I have a form with a select box that allows multiple options. After a user saves these options, it stores
How to create an array of object literals in a loop?
I need to create an array of object literals like this: In a loop like this: The value of key should be results[i].label in each element of the array. Answer