parse string in javascript code example

Example 1: string to array javascript

const str = 'Hello!';

console.log(Array.from(str)); //  ["H", "e", "l", "l", "o", "!"]

Example 2: javascript parse to string

var bar = foo.toString();

Example 3: javascript split

// bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550

var arr = foo.split(''); 
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]

Example 4: split() javascript

let countWords = function(sentence){
    return sentence.split(' ').length;
}

console.log(countWords('Type any sentence here'));

//result will be '4'(for words in the sentence)

Example 5: parse string javascript

parseFloat("16.5"); // 16.5
parseInt("34.6"); // 34
JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); // {name: "John", age: 30, city: "New York"}

Tags:

Php Example