So I am sure if someone ever reads this blog, they will think that I am a bit all over the place with my learning, but there is a method to my madness 😉. As I was doing forms for my Django forms, I found that I was learning a lot but there were things that were escaping me. Could I make a form and make my site work? Yes.  Could I truly say that I knew what I was doing, sadly, No.

So I called in the big guns… I talked to my son-in-law (graduated top of his class from University), my daughter has 10 credits to finish her Software engineering degree (She also has a BS in Asian Studies), my son who has bootcamp in certificate for DJango and Python but pivoted on that knowledge and is working as a Junior Software engineer using C#, after completing an internship and was hired fulltime at an insurance company and now has just over a year done with his degree… So I have a pretty good team that I can ask questions to and a family that this dad is proud of 😊.

The challenge was, “Which version of Javascript should I use?”

This started a discussion about the some different versions and ultimately I decided on focusing on React JS. So I turned to the #1 place to learn… YouTube.

I found a channel that looked promising and going through the first thee lessons but again that feeling of, I am completing Yes, but am I fully understanding? No came back.

So number 2 place that I go when I want to learn something “Udemy”.  <sarcam>And luckly they were having a sale, that never happens. </sarcasm> 😉.

I found two courses that intrested me has I was focused on JS.

  • The Complete JavaScript Course 2023: From Zero to Expert!

  • JavaScript – The Complete Guide 2023 (Beginner + Advanced)

I decided to go with the one that had the a little better rating and more reviews.  I think that either one would have worked well.

 

 

That brings us to Coercion.  In section 2 – Lesson 20 the instructor (Jonas Schmedtmann) states that coercion can make code easier to read without converting everything, but there are others that state that you should convert everything as unless fully understanding coercion the input can cause unexpected bugs. The following example is given:

console.log(2 + 3 + 4 + ‘5’ + 6);

“+” works left to right and since the first three are number type it performs addition but the last value is a string so ‘2+3+4=9 is converted to a string and the values are concatenated and any other additional value will be a string.

I continued this and played with the following which confused me at first as I read it as:

console.log(2 + 3 + 4 + ‘5’ + 6 – 7); = 949

I was reading it at (2+3+4) (‘5’+6) concentrate 7 and was expecting 9567 when the value came out as 949.  Say WHAT?!?!  AI to the rescue… right?

 

 

ME: “how does console.log(2 + 3 + 4 + ‘5’ + 6 – 7); equal 949 in javascript”

AI:

The expression console.log(2 + 3 + 4 + '5' + 6 - 7) is evaluated as follows:

  • First, the numbers 23, and 4 are added together to get 9.
  • Then, the string '5' is concatenated with the number 9 to get '59'.
  • Next, the number 6 is added to '59', which is converted to a number because of the addition operator. This results in the number 65.
  • Finally, the number 7 is subtracted from 65, resulting in the final value of 58.

Therefore, the expression evaluates to 58, not 949

ME after understanding that numbers and string can be converted back and forth: 
This is incorrect information. After the concatenation in step 2 the value is now considered a string so when + 6 is evaluated, + 6 is considered a string and it makes the value of 956 not 58.

AI: 
You are correct. I apologize for the confusion. The expression console.log(2 + 3 + 4 + '5' + 6 - 7) is evaluated as follows:

  • First, the numbers 23, and 4 are added together to get 9.
  • Then, the string '5' is concatenated with the number 9 to get '59'.
  • Next, the number 6 is added to '59', which is converted to a number because of the addition operator. This results in the number 65.
  • Finally, the number 7 is subtracted from 65, resulting in the final value of 58.

Therefore, the expression evaluates to 58, not 94912

Thank you for bringing this to my attention3

ME: You reponse is still incorrect

AI: 
You are correct. I apologize for the confusion. The expression console.log(2 + 3 + 4 + '5' + 6 - 7) is evaluated as follows:

  • First, the numbers 23, and 4 are added together to get 9.
  • Then, the string '5' is concatenated with the number 9 to get '95'.
  • Next, the number 6 is added to '95', which is converted to a string because of the concatenation operator. This results in the string '956'.
  • Finally, the number 7 is subtracted from 956, resulting in the final value of 949.

Therefore, the expression evaluates to 94912

Thank you for bringing this to my attention3

 

 

So two lessons learned coercion converts to the next operator and don’t blindly trust A.I. (I already knew this one…)

And as of now, I am leaning toward converting and not relying on coercing.