How to append something to an array? How do I append to an array in JavaScript? Solution: Use the push() function to append to an array: // initialize array var arr = [ "Hi", "Hello", "Bonjour" ]; // append new value to the array arr.push("Hola"); Now array is var arr = [ "Hi", "Hello", "Bonjour", "Hola" ]; // append multiple values to the array arr.push("Salut", "Hey"); Now array is var arr = [ "Hi", "Hello", "Bonjour", "Hola", "Salut", "Hey" ]; // display all values for (var
undefined
How to replace all occurrences of a string in JavaScript? I have this string: "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 the string above. How can I replace all occurrences of it? Answer: For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this
undefined
How to decide when to use Node.js? I am new to this kind of stuff, but lately I've been hearing a lot about how good Node.js is. Considering how much I love working with jQuery and JavaScript in general, I can't help but wonder how to decide when to use Node.js. The web application I have in mind is something like Bitly - takes some content, archives it. From all the homework I have been doing in the last few days, I obtained the following information. Node.js is a
undefined
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 -> The Eiffel Tower /index.html -> /index.html Most Best Solution: Another solution: function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } You could also add it to the String.prototype so you could chain it with other methods:
undefined
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 two methods? When is it best to use call over apply and vice versa? Solution: The difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. A useful mnemonic is "A for array and C for comma." See MDN's documentation on apply and call. Pseudo
undefined
Validate email address in JavaScript? How can an email address be validated in JavaScript? Solution : Using regular expressions is probably the best way. You can see a bunch of tests here (taken from chromium) function validateEmail(email) { var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } Here's the example of regular expresion that accepts unicode: var re = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; But keep in mind that one should not rely only upon JavaScript validation. JavaScript can easily be disabled. This should be validated on the server side as well. Here's an