jscodetalk-blog
jscodetalk-blog
JavaScript Code Talk
5 posts
JavaScript code examples.
Don't wanna be here? Send us removal request.
jscodetalk-blog · 8 years ago
Text
Convert Array To String
To convert an array to string in JavaSCript you can simply use the .join method. This method has an optional parameter for the separator string. If you do not set it the default comma will be used.
Example:
1 2 3 4 5 6
var namesArray=["Jack", "Jill", "Bob","Tom"]; var namesString = namesArray.join(); var namesString2 = namesArray.join(" "); //result
//namesString = "Jack,Jill,Bob,Tom"
//namesString2 = "Jack Jill Bob Tom "
0 notes
jscodetalk-blog · 8 years ago
Text
How To Merge Two Arrays
In JavaScript the concat() method is used to merge two or more arrays. This method creates a new array by merging existing arrays.
Example:
1 2 3
var arr1 = ['Bob', 'Jim', 'Jack']; var arr2 = ['Jill', 'Pam', 'Liz']; var arr3 = arr1.concat(arr2);
0 notes
jscodetalk-blog · 8 years ago
Text
How To Reverse A String With Built-In Functions
In JavaScript there is no single built-in function to reverse a string so we will use three functions:
1. split(): this will split a String object into a string array 2. reverse(): this will reverse an array 3. join(): this will join all elements of an array into a string.
Here is a complete example:
1 2 3
function reverseString(str) { return str.split("").reverse().join(""); }
0 notes
jscodetalk-blog · 8 years ago
Text
How To Replace All String Occurrences In JavaScript ?
JavaScript's replace function only removes the first occurrence of the string. To replace of occurrences of a string in another string we can use Regular expressions.
Here is an example of a function that replaces all string occurrences in another string:
1 2 3
function replaceAll(originalString, stringToFind, stringToReplace) { return originalString.replace(new RegExp(escapeRegExp(stringToFind), 'g'), stringToReplace); }
0 notes
jscodetalk-blog · 8 years ago
Text
How to process each letter of a string
Here is a simple solution to loop trough a string an process each character. Code:
1 2 3 4 5 6
var str="My string to process." for (var x = 0; x < str.length; x++) { var c = str.charAt(x); alert(c); }
0 notes