惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
S
SegmentFault 最新的问题
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
雷峰网
雷峰网
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Internal Logics Behind Javascript Array Methods
Ezhil Abinaya K · 2026-06-24 · via DEV Community
Cover image for Internal Logics Behind Javascript Array Methods

Ezhil Abinaya K

Array join()
It combines all the elements of an array into a single string.It allows us to specify our own separator between the elements.

const myArr=["HTML","CSS","JS"];
function joinDemo(arr,separator){
let result="";
for(let i=0;i<arr.length;i++){
result+=arr[i];
if(i<arr.length-1){
result+=separator;
}
}
return result;
}
console.log(joinDemo(myArr,"-"));//HTML-CSS-JS

Built-in Logic

const arr=["HTML","CSS","JS"];
console.log(arr.join("-"));//HTML-CSS-JS

Array at()
Returns the value at a specific array index, supporting both positive and negative indexing.

const myArr=["HTML","CSS","JS"];
function atDemo(arr,index){
if(index<0){
index=arr.length+index;
}
return arr[index];
}
console.log(atDemo(myArr,1));//CSS
console.log(atDemo(myArr,1));//JS

Built-in Logic

const arr=["HTML","CSS","JS"];
console.log(arr.at(1));//CSS
console.log(arr.at(-1));//JS

Array pop()
The pop() method removes the last element from an array and returns that removed element.

const myArr=["HTML","CSS","JS"];
function popDemo(arr){
    const removedElement=arr[arr.length-1];
    arr.length=arr.length-1;
    return removedElement;
}
console.log(popDemo(myArr));//JS
console.log(myArr);//[ 'HTML', 'CSS' ]

Built-in Logic

const arr=["HTML","CSS","JS"];
const removed=arr.pop();
console.log(removed);//JS
console.log(arr);//[ 'HTML', 'CSS' ]

Array push()
The push() method adds one or more elements to the end of an array.
Return type: New array length.

const myArr=["HTML","CSS"];
function pushDemo(arr,value){
arr[arr.length]=value;
return arr.length;
}
console.log(pushDemo(myArr,"JS"));//3
console.log(myArr);//[ 'HTML', 'CSS', 'JS' ]

Built-in Logic

const arr=["HTML","CSS"];
const newlength=arr.push("JS");
console.log(newlength);//3
console.log(arr);//[ 'HTML', 'CSS', 'JS' ]

Multiple Values Version

const myArr=["HTML"];
function pushDemo(arr,...values){
    for(let i=0;i<values.length;i++){
        arr[arr.length]=values[i];
    }
    return arr.length;
}
console.log(pushDemo(myArr,"CSS","JS"));//3
console.log(myArr);  //[ 'HTML', 'CSS', 'JS' ]

Array shift()
The shift() method removes the first element from an array and returns that removed element.

const arr=["HTML","CSS","JS"];
function shiftDemo(arr){
    const removedElement=arr[0];
    for(let i=0;i<arr.length-1;i++){
        arr[i]=arr[i+1];
    }
    arr.length=arr.length-1;
    return removedElement;
    }
console.log(shiftDemo(arr));//HTML
console.log(arr);//[ 'CSS', 'JS' ]

Built-in Logic

const arr=["HTML","CSS","JS"];
const removed =arr.shift();
console.log(removed);//HTML
console.log(arr);//[ 'CSS', 'JS' ]

Array unshift()
The unshift() method adds one or more elements to the beginning of an array and returns the new length of that array.

const myArr=["CSS","JS"];
function unshiftDemo(arr,value){
for(let i=arr.length;i>0;i--){
arr[i]=arr[i-1]; 
}
arr[0]=value;
return arr.length;
}
console.log(unshiftDemo(myArr,"HTML"));//3
console.log(myArr);//[ 'HTML', 'CSS', 'JS' ]

Built-in Logic

const arr=["CSS","JS"];
const newLength=arr.unshift("HTML");
console.log(newLength);//3
console.log(arr);//[ 'HTML', 'CSS', 'JS' ]

Array concat()
The concat() method in JavaScript merges two or more arrays and returns a brand-new array without modifying the original arrays.

const arr1=["HTML","CSS"];
const arr2=["JS","React"];
function concatDemo(arr1,arr2){
const result=[];
for(let i=0;i<arr1.length;i++){
result[result.length]=arr1[i];
}
for(let i=0;i<arr2.length;i++){
result[result.length]=arr2[i];
}
return result;
}
console.log(concatDemo(arr1,arr2));//[ 'HTML', 'CSS', 'JS', 'React' ]

Built-in Logic

const arr1=["HTML","CSS"];
const arr2=["JS","React"];
const result=arr1.concat(arr2);
console.log(result);//[ 'HTML', 'CSS', 'JS', 'React' ]

Array copyWithin()
The copyWithin() method copies array elements to another position within the same array. It directly modifies (mutates) the original array without changing its length.
Syntax

array.copyWithin(target, start, end)

target → The index where the copied elements will be pasted.
start → The index from which elements will be copied.
end → The index up to which elements will be copied (exclusive, not included).

const myArr=[1,2,3,4,5];
function copyWithinDemo(arr,target,start){
for(let i=start;i<arr.length;i++){
arr[target]=arr[i];
target++;
}
return arr;
}
console.log(copyWithinDemo(myArr,0,3));//[ 4, 5, 3, 4, 5 ]

Built-in Logic

const myArr=[1,2,3,4,5];
myArr.copyWithin(0,3);
console.log(myArr);//[ 4, 5, 3, 4, 5 ]

Array flat()
The flat() method creates a new array with all sub-array elements concatenated into it recursively up to a specified depth.

const arr = [1, 2, [3, 4], [5, 6]];

function flatDemo(arr) {

    const result = [];

    for (let i = 0; i < arr.length; i++) {

        if (Array.isArray(arr[i])) {

            for (let j = 0; j < arr[i].length; j++) {
                result.push(arr[i][j]);
            }

        } else {

            result.push(arr[i]);

        }
    }

    return result;
}

console.log(flatDemo(arr));//[ 1, 2, 3, 4, 5, 6 ]

Built-in Logic

const arr = [1, 2, [3, 4], [5, 6]];
console.log(arr.flat());//[ 1, 2, 3, 4, 5, 6 ]

Array slice()
The slice() method copies a portion of an array and returns it as a new array without modifying the original one.
Syntax

array.slice(startIndex, endIndex);

const arr=["HTML","CSS","JS","React"];
function sliceDemo(arr,start,end){
const result=[];
for(let i=start;i<end;i++){
result[result.length]=arr[i];
}
return result;
}
console.log(sliceDemo(arr,1,3));//[ 'CSS', 'JS' ]
console.log(arr);//[ 'HTML', 'CSS', 'JS', 'React' ]

Built-in Logic

const arr=["HTML","CSS","JS","React"];
const result=arr.slice(1,3);
console.log(result);//[ 'CSS', 'JS' ]