Javascript converts a string into an array like php explode
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:
How to preview link with iframe and javascript?
Sunday 10th of January 2021 05:58:27 PM
javascript
How to add class and remove class in javascript?
Thursday 7th of January 2021 03:35:21 PM
Javascript