Javascript questions for you javascript tards | |
Anonymous Coward User ID: 78008151 04/21/2021 09:44 PM Report Abusive Post Report Copyright Violation | |
Anonymous Coward User ID: 38472722 United Kingdom 04/21/2021 09:51 PM Report Abusive Post Report Copyright Violation | This where having done a proper language like C would stand you in good stead. If you try to compare the two arrays like that with == or === you are comparing the references of the objects, not the content of the objects. When you create an object it’s give a reference, for simplicity’s sake... array1 = []; // assigned reference x array2 = []; // assigned reference y x != y = is assignment, I.e. a = x. Whatever x is, a is now equal to, type, or object. == is equality with conversion essentially, I.e. 1 == ‘1’ // true === is strict equality, I.e. 1 === ‘1’ // False |
fiora.ni
User ID: 78314149 United States 04/21/2021 10:01 PM Report Abusive Post Report Copyright Violation | I've been playing with javascript for two decades, but more so in the past few years. This one popped up in my programming and just threw me for a loop. I understand it now, but I'm still left scratching my head over something that I should have known already, though on the other hand my natural approach seemed like it should have worked? Quoting: BoatyMcBoatface Why can't I compare two objects to each other with normal comparisons? For example: let array1 = [] let array2 = [] console.log(array1 == array2) // false console.log(array1 === array2) // false Why when I set array1 to equal array2, if I change either array1 or array2 both array1 and array2 equal each other immediately? Why do objects like this behave so differently than every other type of variable in javascript? Sidebar: why are javascript equality operators so f'ing weird (=, ==, ===)? 1. google `lodash.isEqual` and use it 2.1. the two arrays are not equal because they are two different objects, even though their content may be the same. 2.2. javascript objects are references(in c++ terms they are pointers in memory) 3. === is comparison operation + comparison by type Current state of affairs: "In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move." Russian warship, go f#ck yourself! Say with me: Palianytsia :) |
BoatyMcBoatface
(OP) User ID: 77825331 United States 04/21/2021 10:24 PM Report Abusive Post Report Copyright Violation | I've been playing with javascript for two decades, but more so in the past few years. This one popped up in my programming and just threw me for a loop. I understand it now, but I'm still left scratching my head over something that I should have known already, though on the other hand my natural approach seemed like it should have worked? Quoting: BoatyMcBoatface Why can't I compare two objects to each other with normal comparisons? For example: let array1 = [] let array2 = [] console.log(array1 == array2) // false console.log(array1 === array2) // false Why when I set array1 to equal array2, if I change either array1 or array2 both array1 and array2 equal each other immediately? Why do objects like this behave so differently than every other type of variable in javascript? Sidebar: why are javascript equality operators so f'ing weird (=, ==, ===)? 1. google `lodash.isEqual` and use it 2.1. the two arrays are not equal because they are two different objects, even though their content may be the same. 2.2. javascript objects are references(in c++ terms they are pointers in memory) 3. === is comparison operation + comparison by type I found loadash as part of my struggles that finally gave me the breakthrough I needed with my code. I regret not taking the 2nd semester in computer science in highschool where I would have learned C++... but here I am. ```````````````` ````__/\__`````` ~~~\____/~~~~ .~~..~~~....~~~ ~..~~~....~~~~ Thoughts do not come from you nor God; you do not create thoughts; you are not your thoughts; every thought is a lie. - 2 Corinthians 10:5 - [link to www.biblegateway.com (secure)] |
Anonymous Coward User ID: 79336278 United States 04/11/2023 11:44 AM Report Abusive Post Report Copyright Violation | |
Anonymous Coward User ID: 72105818 United States 04/11/2023 11:51 AM Report Abusive Post Report Copyright Violation | |
Anonymous Coward User ID: 83089578 United States 04/11/2023 11:59 AM Report Abusive Post Report Copyright Violation | |
MaybeTrollingU
User ID: 84679204 Brazil 04/11/2023 12:03 PM Report Abusive Post Report Copyright Violation | I've been playing with javascript for two decades, but more so in the past few years. This one popped up in my programming and just threw me for a loop. I understand it now, but I'm still left scratching my head over something that I should have known already, though on the other hand my natural approach seemed like it should have worked? Quoting: BoatyMcBoatface Why can't I compare two objects to each other with normal comparisons? For example: let array1 = [] let array2 = [] console.log(array1 == array2) // false console.log(array1 === array2) // false Why when I set array1 to equal array2, if I change either array1 or array2 both array1 and array2 equal each other immediately? Why do objects like this behave so differently than every other type of variable in javascript? Sidebar: why are javascript equality operators so f'ing weird (=, ==, ===)? 1. google `lodash.isEqual` and use it 2.1. the two arrays are not equal because they are two different objects, even though their content may be the same. 2.2. javascript objects are references(in c++ terms they are pointers in memory) 3. === is comparison operation + comparison by type I personally, use stringify to compare arrays contents. But your responses are on point, so OP, that's pretty much it. |