string.parse javascript code example

Example 1: string split javascript

var myString = "An,array,in,a,string,separated,by,a,comma";
var myArray = myString.split(",");
/* 
*
*  myArray :
*  ['An', 'array', 'in', 'a', 'string', 'separated', 'by', 'a', 'comma']
*
*/

Example 2: javascript split string

// Split string into an array of strings
const path = "/usr/home/chucknorris"
let result = path.split("/")
console.log(result)
// result
// ["", "usr", "home", "chucknorris"]
let username = result[3]
console.log(username)
// username
// "chucknorris"

Example 3: 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