JavaScript Array join() and isArray()

In this post, we will discuss join(),isArray() methods available in JavaScript Array.

join()

The join() method will return a string by joining the elements in an array with a separator. If will take this a parameter. If the separator is not specified, by default elements are separated by a comma separator.

Syntax:

array_name.join(separator)

Here, 'array_name' is the name of the array. It will take separator as a parameter.

Example 1:-

Let's create an array that hold 10 subjects and return all elements separated by a comma.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Join the elements in the above array.
document.writeln("Join: <br>");
document.writeln(subjects1.join());
</script>
</body>
</html>

Output:

Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Join:
php,html,node.js,java,jsp,jsp,php,php,java,php

We can see that all the elements were joined by a separator comma. Here you can observe we did not pass any parameter to join() method.

Example 2:-

Let's create an array that hold 10 subjects and return all elements separated by "->".

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
// Join the elements in the above array separated by "->".
document.writeln("Join: <br>");
document.writeln(subjects1.join("->"));
</script>
</body>
</html>

Output:

Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Join:
php->html->node.js->java->jsp->jsp->php->php->java->php

We can see that all the elements were joined by a separator - "->".

isArray()

The isArray() method is used to check whether the given object is Array or not. If it is array, true is returned, otherwise false is returned.

Syntax:

Array.isArray(array_name)

Here, 'array_name' is the name of the array. It will take 'array_name' as a parameter.

Example 1:-

Let's create an array that hold 10 subjects and check if it is array or not.

CopiedCopy Code
<html>
<body>
<script>
// Create an array that hold 10 strings
const subjects1 =["php","html","node.js","java","jsp","jsp","php","php","java","php"];
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
document.writeln("Is subjects1 an array?: <br>");
document.writeln(Array.isArray(subjects1));
</script>
</body>
</html>

Output:

Subjects:
php,html,node.js,java,jsp,jsp,php,php,java,php
Is subjects1 an array?:
true

We can see that subjects1 is an Array.

Example 2:-

Let's create a string and check if it is array or not.

CopiedCopy Code
<html>
<body>
<script>
// Create string
const subjects1 = "string"
document.writeln("Subject: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
document.writeln("Is subjects1 an array?: <br>");
document.writeln(Array.isArray(subjects1));
</script>
</body>
</html>

Output:

Subject:
string
Is subjects1 an array?:
false

We can see that subjects1 is not an array. Since false is returned.

Example 3:-

Let's create a Set with 5 elements and check if it is array or not.

CopiedCopy Code
<html>
<body>
<script>
// Create a Set with 5 elements
const subjects1 = new Set([23,45,67,54,33]);
document.writeln("Subjects: <br>");
document.writeln(subjects1);
document.writeln("<br>");
document.writeln("<br>");
document.writeln("Is subjects1 an array?: <br>");
document.writeln(Array.isArray(subjects1));
</script>
</body>
</html>

Output:

Subjects:
[object Set]
Is subjects1 an array?:
false

We can see that subjects1 is not an array. Since false is returned.