Updated April 7, 2023
Introduction to jQuery array to string
The jQuery array toString() function is used to returns the array into the string form. The jQuery array toString() function is a built-in function in jQuery for the array object. The jQuery array toString() function converts and returns a string with all the array values, where the commas separate each value, but the array toString() function doesn’t change the original array.
The syntax of the jQuery array toString() function –
array.toString();
Parameters:
The jQuery array toString() function doesn’t accept any parameters.
Return value:
This function’s return value is the string that contains all the specified array elements with the comma-separated.
Working of the JQuery array toString() function
The JQuery array toString() function is used to return the string form of an array; it doesn’t accept any parameter. Suppose we have an array of numbers as “no = [ 51, 20, 64, 89, 45, 25 ]”; now we need to get the string form of a “no” array. So we can use the array toString() function as “var str = no. toString();”, where str is a variable store the return sting result of toString() function that is “51, 20, 64, 89, 45, 25”.
Examples of the jQuery array toString() function
Here are the following examples mention below
Example #1
Example of jQuery array toString() function to get all student’s marks in the string form in the given array –
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery array toString() function </title>
<style>
#p1 {
color: blue;
}
#p2 {
color: green;
}
#p3 {
color: red;
}
</style>
</head>
<body>
<h3> This is an Example for array toString() function : </h3>
<button onclick = "checkRes()" > Click here to get all the marks in the sring form. </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<p id = "p3"> </p>
<script>
var marks = [ 50, 78, 40, 89, 60, 57, 90 ];
function checkRes()
{
$( "#p1" ).text("The marks of an given array before toString() function is : " + marks);
$( "#p1" ).append(". The type of the marks array is : " +jQuery.type(marks));
var res = marks.toString();
$( "#p2" ).text("The string form of marks array after toString() function is : " + res);
$( "#p2" ).append(". The type of the string marks array is : " +jQuery.type(res));
$( "#p3" ).text("The marks of a original array after toString() function is : " + marks);
}
</script>
</body>
</html>
Output:
Once we click on the button, the output is –
In the above code, the array is created, which contains marks of the students. Next, the toString() function is used to return the string form of the array by using the toString() function as “marks.toString();”, as we can see in the above output.
Example #2
Example of jQuery array toString() function to get all student’s names in the string form in the given array.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery array toString() function </title>
<style>
#p1 {
color : blue;
}
#p2 {
color : red;
}
</style>
</head>
<body>
<h3> This is an Example for array toString() function : </h3>
<button onclick = "checkRes()" > Click here to get all the student names. </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<script>
var Student = [ "Smith", "Frank", "Reily", "Patel", "John" ];
function checkRes()
{
alert( "The list of Students from the array are : " + Student+". \nThe type of the Student array is : " +jQuery.type(Student));
var res = Student.toString();
alert("The list of Students in the string form : " + res+ ".\n The type of result return from toString() function is : " +jQuery.type(res));
}
</script>
</body>
</html>
Output:
Once we click on the button, the output is –
Once we click ok, the output is –
In the above code, the array is created, which contains the name of the students. Next, the array toString() function is used to get the string form of the student array as “Student.toString();” the toString() function returns the string form of the Student array, but it doesn’t change the original Student array, as we can see in the above output.
Example #3
Example of jQuery array toString() function to get all the subjects in the string form in the given array and to check whether we access the elements of the array from the string form –
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<title> This is an example for jQuery array toString() function </title>
<style>
#p1 {
color : blue;
}
#p2 {
color : red;
}
</style>
</head>
<body>
<h3> This is an Example for array toString() function : </h3>
<p id = "p1"> </p>
<p id = "p2"> </p>
<p id = "p3"> </p>
<button onclick = "checkRes()" style = "background-color : green" > Click here to get the string form of the subject array. </button>
<script>
var arr = new Array( "JavaScript", "CSS", "Java", "jQuery");
function checkRes()
{
$( "#p1" ).text("The marks of an given array before toString() function is : " + arr);
$( "#p1" ).append(". The type of the marks array is : " +jQuery.type(arr));
var res = arr.toString();
$( "#p2" ).text("The string form of marks array after toString() function is : " + res);
$( "#p2" ).append(". The type of the string marks array is : " +jQuery.type(res));
$( "#p3" ).append( "arr[1] " +arr[1] + "<br>" );
$( "#p3" ).append( "res[1] " +res[1] + "<br>" );
}
</script>
</script>
</body>
</html>
Output:
Once we click on the button, the output is –
In the above code, the array is created, which contains the name of the subjects. Next, the array toString() function is used to get the string form of the subject array and try to get the array element from the string, as now it is a string, so it returns only one character in the output which is present at that location, as we can see in the above output.
Conclusion
The jQuery array toString() function is a predefined function used to return the given array into the string form.
Recommended Articles
This is a guide to jQuery array to string. Here we discuss the Working of the JQuery array toString() function along with the examples and outputs. You may also have a look at the following articles to learn more –