Learn html, css, javascript online and how to make a webpage design

Javascript converts a string into an array like php explode

Saturday 2nd of January 2021 10:06:57 PMjavascriptstring to arrayarray to string

Sometimes when you switch from php language to javascript you need the same functions from one language to another. Specifically, the function that you use most often is the function that converts the string to an array and vice versa converting the array to string.

String to Array

<!DOCTYPE html>
<html>
<head>
    <title>Javascript converts a string into an array like php explode? - HtmlcssDownload.com</title>
</head>
<body>
    
<div id="result"></div>
   
<script type="text/javascript">
     
    var myString = 'Javascript,PHP,HTML,CSS';
    var arrayFromString = myString.split(',');
    console.log(arrayFromString);

    // result is Array
    // ["Javascript", "PHP", "HTML", "CSS"]
    
</script>
    
</body>
</html>

 

Array to String

<!DOCTYPE html>
<html>
<head>
    <title>Javascript converts Array to String - HtmlcssDownload.com</title>
</head>
<body>
    
<div id="result"></div>
   
<script type="text/javascript">
     
    var myArray = ["Javascript", "PHP", "HTML", "CSS"];
    var stringFromArray = myArray.join(',');
    console.log(stringFromArray);

    // result is String
    // "Javascript,PHP,HTML,CSS"
    
</script>
    
</body>
</html>
We are Recommending you: