Why is (a + b) not equal to (b + a)?

Tell us what’s happening:
Why am I having the console error: “TypeError: “length” is read-only” when I write

function findLongestWordLength(str) {
  let splitPhrase = str.split(" ");
  let longestWord = 0;
  for(let i = 0; i < splitPhrase.length; i++) {
    if(splitPhrase[i].length > longestWord) {
    splitPhrase[i].length = longestWord;
   }
  } 
  return longestWord;
}

and not when the equal expression is switched to:

longestWord =  splitPhrase[i].length;

This is a basic math principle in which the order of factors should not affect the result when it comes to sums or multiplications, meaning (a + b) should be equal to (b + a), however this is not the case in this expression. Why?

function findLongestWordLength(str) {
let splitPhrase = str.split(" ");
let longestWord = 0;
for(let i = 0; i < splitPhrase.length; i++) {
  if(splitPhrase[i].length > longestWord) {
  splitPhrase[i].length = longestWord;
 }
} 
return longestWord;
}

console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0

Challenge: Find the Longest Word in a String

Link to the challenge:

In programming, a + b is (within floating point error) equal to b + a.

But a = b is very different than b = a. The = is the assignment operator. a = b says 'assign the value of b to a’ while b = a says 'assign the value of a to b'. You can’t assign a value to myString.length or my Array.length because those properties describe a string/array, but that value can be stored in another variable.

The strict comparison operator, ===, works as you expect though, providing the same results for a === b and b === a. (Order matters in weak comparison, ==, though)

1 Like

As Jeremy said, the = operator is the assignment operator.

Is the command to save the value of longestWord into xplitPhrase[i].length, which is a syntax error.

Is the command to save the value of splitPhrase[i].length into the variable longestWord, which is perfectly valid.

2 Likes

You can change the length property on an array you can’t do it on a string.

Also, in strict mode you will get an error if you try to assign to the length property on a string (the challenges run in strict mode). You won’t get that error otherwise (it just won’t work).

function strictMode() {
  'use strict';
  const arr = [1, 2, 3];
  arr.length = 0;

  console.log(arr); // []

  const name = 'John';
  name.length = 0; // Cannot assign to read only property 'length' of string 'John'
}

strictMode();

Huh, I didn’t know you could manually change the length of an array. That feels fundamentally wrong. Now I’m going to see if you can also artificially increase the size of the array.

Edit: Yup, you can. This just feels so… ewww…

I assume it’s just that arrays are mutable and strings are immutable.

You will sometimes see the array length get changed in code but I don’t think it is used all that often anymore. Pretty sure it is considered a bad practice by most.

23.1.4 Properties of Array Instances

22.1.4 Properties of String Instances


As an aside, technically, anything that has properties on it is an object, the same goes for functions. They just do not look like objects. There are also differences between the version of the type (boxed/unboxed).

function okSure() {
  console.log(okSure.someProp); // Hmm
}

okSure.someProp = 'Hmm';
okSure();

// Does it work on a string literal?

const someString = 'What now?'
someString.canWe = 'no'

console.log(someString.canWe); // undefined

// What about when using the constructor?

const oString = new String('What about this?')
oString.howAboutNow = 'yes we can'

console.log(oString.howAboutNow); // yes we can

// Still can't change the length though

console.log(oString.length); // 16
oString.length = 0;
console.log(oString.length); // 16
1 Like

Yes I get your point now, this expression is not meant to state equality but to assign a value to the already declared function, aka longestWord. Thank you!

1 Like