Liliia Dokuchaeva’s Post

Got a minute for some JavaScript? 🍵 What does this code output? Answers 🔍 >>> - ReferenceError: message is not defined Why? Because let lives only inside the block where it’s created. In this code, message is created inside the *if {}* and *else {}* blocks. When JavaScript reaches *console.log(message)*, it is already outside those blocks, so the variable no longer exists. #javascript #webdevelopment

  • text

love this trash, checking whos brain can compete with compilator. instead of giving something valuable.also this is not a fact. message could be also globaly defined with some random content so probably it could also output global variable, it is also an option. But in reality the outcome of those lines can even depend on multiple reasons you could even add some interpreter that rewrites let to support let variables until function endings. I just doesn't understand what those guys are trying to achive. (also it could even depend on javascript compiler, some will write undefined some may write ReferenceError, even shut up is possible.)

I forgot this rule of "if" scope. In the meanwhile there is two responsibilities in this function:- resolve the message- display itSo by applying SRP, no more errors, no more confusion.const checkDog = (animal) => animal === 'dog' ? "I love dog": "That's not a dog";console.log(checkDog("dog"))

see this please

  • No alternative text description for this image

Or replace for “var” but Im not recommending 😅

Like
Reply

function checkAnimal(animal) { let message; if (animal === "dog") { message = "I love dogs!"; } else { message = "That's not a dog."; } console.log(message); }

Considering it's JavaScript, it could be anything! :)

ReferenceError: message is not defined because in both cases is defined as local for the corresponding block- Afyer block ends, message does not exist...

ReferenceError: message is not defined

See more comments

To view or add a comment, sign in

Explore content categories