Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Javascript Array[Variable] Reference Problems

let string = ['h', 'e', 'l', 'l', 'o'];
let count = 0;
let part = string[count];
console.log(part); // logs out 'h'
count ++;
console.log(part); // still loggs out 'h'

I would like for the second log to log out 'e' instead of 'h'. Because I've updated code to 1 instead of 0.

Edit: I understand that I can just keep using the string[count] reference but I was asking if there was a way to do what I have described with a variable. (Make it reactive)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

when you initialize part, you are setting it equal to 'h'. Despite incrementing count, that will not change part as it is still h. Thus, you will need to set part equal to string[count] again, after incrementing count.

let string = ['h', 'e', 'l', 'l', 'o'];
let count = 0;
let part = string[count];
console.log(part); // logs out 'h'
count ++;
part = string[count];
console.log(part); // now logs 'e'

about 4 years ago · Juan Pablo Isaza Report

0

You can make part a function, then call it whenever you want to get the item at index count:

let string = ['h', 'e', 'l', 'l', 'o'];
let count = 0;
let part = () => string[count];
console.log(part());
count ++;
console.log(part());

about 4 years ago · Juan Pablo Isaza Report

0

If you want it dynamic based off counter don't create the part reference.

let string = ['h', 'e', 'l', 'l', 'o'];
let count = 0;
console.log(string[count]) // logs out 'h'
count ++
console.log(string[count])

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!