Scroll
Javascript Lesson 5 Arrays, Functions for Working with Arrays
In this lesson, we’ll explore another JavaScript data type: arrays. We’ll also look at some functions for working with strings and arrays. The rest of the functions will be covered in the following lessons. For an introduction to arrays, you can check out the materials on PHP arrays:
PHP Lessons – Lesson 10 – Arrays
PHP Lessons – Lesson 11 – String and Array Functions
join() Method
The join()
method combines all elements of an array into a single string, separated by a specified delimiter. If no delimiter is provided, a comma is used by default.
let fruits = ["Apple", "Banana", "Cherry"];
let result = fruits.join(" - ");
console.log(result); // "Apple - Banana - Cherry"
split() Method
The split()
method splits a string into an array of substrings based on a specified delimiter.
let text = "one,two,three";
let parts = text.split(",");
console.log(parts); // ["one", "two", "three"]
length Property
The length
property returns the number of elements in an array or the number of characters in a string.
let arr = [10, 20, 30];
console.log(arr.length); // 3
let str = "Hello";
console.log(str.length); // 5