JavaScript doesn’t have a reverse() method for strings but has one for arrays. The trick is to convert a string to an array, reverse the array and combine the array back into a string:
var s = "Hello World!";
var reversed = s.split("").reverse().join("");
// "!dlroW olleH"
Suppose you already have some items in an array and you want to append those items to a target array. You can use the concat method to create a new array that is the concatenation of the two arrays and replace the target array with the result:
var items = [ 3, 5, 7 ];
target = target.concat(items); // new instance!
You can avoid replacing the array with a new instance every time you need to append new items to it by using the push method. One thing to note is that push accepts multiple arguments and we can apply the new item array as arguments to push directly:
Array.prototype.push.apply(target, items);
// Equivalent to target.push(3, 5, 7)
// -- if the items weren't already in an array
If you want to clear a property of an object, simply setting it to undefined won’t always cut it, based on how you will access/test the property later on:
// The setup
var obj = {
prop: 1
};
// Clear property value
obj.prop = undefined;
The property is still attached to the object as a property and can be enumerated:
for (var key in obj) {
// Will iterate through "prop"
}
typeof obj.prop == "undefined" // true
obj.hasOwnProperty("prop") // true -- it's still there!
To completely eliminate a property, you need to delete it:
delete obj.prop;
for (var key in obj) {
// Won't iterate through "prop" anymore
}
typeof obj.prop == "undefined" // true
obj.hasOwnProperty("prop") // false -- it's gone completely!