#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Arrays and Strings

Arrays and strings are the two kinds of sequence you will handle most in JavaScript. A string is an ordered sequence of characters; an array is an ordered list of any values. They share several features, such as indexes and a length property, but differ in one crucial way: strings cannot be changed in place, while arrays can.

What they have in common

Both use zero-based indexes. The first item is at position 0 and the last at length - 1. Both support includes, indexOf, slice, concat and the newer at method, which accepts negative numbers to count from the end. You can loop over both with for...of.

const word = "frontend";
const tools = ["html", "css", "js"];

console.log(word[0], tools[0]);          // "f" "html"
console.log(word.length, tools.length);  // 8 3
console.log(word.at(-1), tools.at(-1));  // "d" "js"
console.log(word.slice(0, 5));           // "front"
console.log(tools.includes("css"));      // true

// Converting between the two
const letters = word.split("");          // ["f","r","o","n","t","e","n","d"]
const csv = tools.join(", ");            // "html, css, js"
const reversed = [...word].reverse().join(""); // "dnetnorf"

Strings are immutable

Once created, a string never changes. Methods like toUpperCase, replace and trim return a new string and leave the original alone. Assigning to an index, such as word[0] = "F", quietly does nothing (or throws in strict mode). To "change" a string you build a new one and store it: word = word[0].toUpperCase() + word.slice(1).

Arrays are mutable

Arrays can be changed directly. push and pop work on the end, unshift and shift on the start, and splice can insert or remove anywhere. You can also assign by index: tools[1] = "sass". Methods such as map, filter and slice do not change the original and return a new array instead.

The sort and reverse surprise

A frequent mistake is assuming every array method returns a copy. sort, reverse and splice modify the original array. If you call const sorted = scores.sort(), both sorted and scores now point to the same reordered array. To keep the original, copy first with [...scores].sort() or use the newer non-mutating versions toSorted and toReversed in modern environments.

A second surprise is that sort compares items as strings by default, so [10, 9, 1].sort() gives [1, 10, 9]. For numbers, pass a compare function: sort((a, b) => a - b).

Moving between strings and arrays

Many string problems are easier as array problems. split breaks a string into an array using a separator, and join glues an array back into a string. Splitting a sentence on spaces gives you words to count or filter; splitting on an empty string gives characters. Spreading a string with [...text] is safer than split("") for emoji and some other characters, because it splits by full characters rather than by UTF-16 code units.

Checking which one you have

typeof returns "string" for strings but "object" for arrays, which is not helpful. Use Array.isArray(value) to test for an array reliably.

Takeaway: arrays and strings share indexes and many methods, but strings are immutable while arrays can be modified, so know which methods change the original and use split and join to move between them.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course