thejsway / thejsway

The JavaScript Way book

Home Page:https://thejsway.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chapter 06 - arrays named "values"

Christina-Milner opened this issue · comments

In the "Sum of values" and "Array maximum" problems at the end of Chapter 6, students are asked to:

Write a program that creates the following array, (...)
const values = [3, 11, 7, 2, 9, 10];

Following these instructions will lead to problems when attempting to do anything with the array, as values is interpreted as the Object.values() method.

const myArray = [1, 2, 3]
myArray.forEach(element => console.log(element))
// behaves as expected

const values = [1, 2, 3]
values.forEach(element => console.log(element))
//TypeError: values.forEach is not a function

Naming the array something else like "numbers" might avoid some confusion.

Sorry for answering late and thanks for reporting this.

However, both code samples work fine in my context and unless I'm mistaken, values is a valid variable name in JavaScript. Could you please be more specific about your JS test environment ?

My apologies, I should have tried different environments/specified. I was using the browser's inbuilt Console (via Inspect), but turns out this is a Firefox issue (tested on 103.0 as well as the version before it), see screenshot.
Not reproduced in Chrome or Edge, and the page the "Learn More" link leads to does say "V8 based, Firefox and Safari": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function

Edit to add: The same behaviour can be observed by naming the array "keys". Will throw a TypeError in Firefox.

Final edit: This error also does not occur when writing this code in a .js file, linking it to an HTML file and opening that in Firefox. Only when entering it directly into the console.

values_js_firefox