Why does JavaScript consider [1,2,3] to not be equal to [1,2,3]?

Can someone explain why [1,2,3] is not the same as [1,2,3] and [1,2,3] in JavaScript? I saw this in a YouTube video and it got me curious. Are there any more interesting quirks like this in coding?

Answer №1

When using the == operator, it verifies if the two operands are pointing to the same object. Even though both objects (arrays being objects) might contain the same data, they are distinct instances, resulting in the expression evaluating as false.

Answer №2

When using the === and == operators to compare arrays, it's important to remember that they are comparing by reference (i.e. location in memory). Essentially, this means that "they are different arrays with the same values".

One way to make this concept clearer is to give your arrays names:

const arr1 = [1, 2, 3];
const arr2 = [1, 2, 3];

console.log(arr1 === arr2);

In this case, arr1 and arr2 are not referencing the same array.

However, if you compare an array to itself, like so:

const arr = [1, 2, 3];

console.log(arr === arr);

It will return true, because it is comparing the same reference.

Answer №3

The reason they are not considered equal is because they do not have the same value. When working with JavaScript, if you create an array like [1,2,3] without assigning it a name, it will just exist as a variable in memory. However, if you give it a name like 'r' and then compare r==r, it will evaluate to true because the reference to window.r is the same as the reference to itself. On the other hand, variables like window.undefinedvariable0 and window.undefinedvariable1 may look visually similar but they are actually not the same in terms of their references.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Collapse or expand nested rows within a dynamic table using Bootstrap Collapse feature

I am currently working on creating a dynamic leaderboard table for a sports league using data from a SQL database. The league consists of multiple teams, each team has different players (with some players belonging to more than one team), and players earn ...

Is it possible to retrieve browser history from Google Chrome using Node.js on a Windows operating system?

I'm currently developing a personal electron app for managing my lifestyle. One of the key features is controlling my daily internet browsing through the app. I am aiming to integrate my Chrome history into the electron app. Could someone recommend a ...

How can one achieve a fading effect on the background using THREE.js?

I am attempting to achieve a fading effect on the drawing buffer while leaving trails of drawn objects, rather than clearing it every frame. It's a relatively simple effect that can be achieved in 2D using this code snippet: http://jsfiddle.net/faRW3/ ...

Adjusting the height of a GAS iframe in WordPress with iframe-resizer: A step-by-step guide

I would like to embed an iframe of my Google Application Script Web into my WordPress site without the scroll bar. Refer to the image below for context. https://i.stack.imgur.com/7L6Tw.png I encountered an error message while attempting to use the iframe ...

Using AJAX to upload an image and passing multiple parameters

I'm facing an issue when trying to upload an image along with other input text in a form and send it to ajax_php_file.php. Whenever I upload the image, all my input text fields appear empty. Any assistance would be greatly appreciated. Thank you very ...

AngularJS Form Validation Error Handling

I am in the process of implementing Form Validation using AngularJS, and I have come across a scenario involving ng-class that is confusing me. Could someone please explain why they are utilizing ng-class in this manner? The presence of a map and an arra ...

Angular Commandments: Understanding the Directives

Within my code, there is a specific directive view that I am utilizing: <div class="busy-indicator angular-animate" ng-show="busy"></div> <div class="breadcrumblist" ng-class="atTopLevel ? ['at-top-level'] : null"> <div ...

Guide: Creating an AngularJS directive for displaying a jQuery dialog that triggers submission on pressing the Enter button

Looking to create a similar functionality to the jQuery modal form Dialog in my AngularJS app to collect the username from the user. I've tried using the twitter-bootstrap modal and AngularUI dialog, but both are lacking in two key areas: Auto-focu ...

Filter a Vue list based on a checkbox that can be either checked or unchecked

I am currently working on my Vue app and aiming to filter a list to display only entries that have been moderated. However, I am encountering an issue where when the checkbox is checked, I receive all the results that are true, and when the checkbox is un ...

Unintended repetition with fetch() for loading a texture

I've been experimenting with using the fetch statement to fetch a local image and incorporate it into my (three.js) project. However, I seem to have created an infinite loop and I can't figure out why. Since the project is quite large, I've ...

Can a script be executed on a node.js module?

I have been developing a node package with an installation script that establishes a basic application structure. This script simply creates a few folders and generates an "admin" user if one does not already exist. Currently, the script performs multiple ...

ReactJS with conditional closing tags

Here is a sample json response : {id: 1, name: a} {id: 2, name: b} {id: 3, name: c} {id: 4, name: d} {id: 5, name: e} {id: 6, name: f} I am looking to organize these by pairs in my React component like so : <div className="group-item"> ...

Having difficulty executing the playwright tests

Trying to execute the playwright test from the specified location results in a message prompting to run npm install -D @playwright/test before running the test. Despite having already installed playwright as a dev dependency, the test is still not being ex ...

Subdomain redirection issue with express-subdomain for localhost GET requests

In order to manage requests to specific subdomains, I am utilizing a package in express.js called express-subdomain. From my understanding, the subdomain constructor function requires an express router object that I pass from an exported router module. M ...

The v-model in Vue does not function correctly when used with a single index within an array

I am encountering a situation with a series of input fields, including a checkbox and a dropdown button in each row. Additionally, there is a button to add a new row of inputs. When the checkbox is checked, it should disable the dropdown menu and make it ...

What is the best way to reset a looping variable in a Node.js application?

I have encountered an issue with an API method in my nodejs app. Within one of my controllers, I am attempting to generate pages upon clicking. However, I have found that when calling the method, my for loop fails to reset the values to their default state ...

Challenges when utilizing AJAX and JQuery for selecting multiple items and retrieving data from a JSON file

I am currently working on a live search feature that extracts data from a JSON file using AJAX and jQuery. The goal is to make the fetched value clickable and populate a multiselect field with it. Once this is achieved, the plan is to capture the remaining ...

The Node/Express Rest API appears to keep directing requests to the same controller function, despite the mappings being correctly

Currently, I am in the process of developing a node/express REST API. When making requests to the following endpoints: http://localhost:5000/api/news and http://localhost:5000/api/news/?id=c5f69d56be40e3b56e55d80 I noticed that both URLs trigger the same ...

Using Redux saga: passing arguments to axios instance

I need to transition my code from thunk to saga to meet the requirements of my company. While it was easy to send api requests with params using thunk, I am struggling to figure out how to pass params to the axios request: redux/sagas/handlers/marketpla ...

Uncover the valuable information within a string using regex in JavaScript

I am looking for a solution to extract key values from a string that looks like this: <!-- Name:Peter Smith --><!-- Age:23 --> My goal is to create a standard function that can extract any value needed. The function call would be something lik ...