3

I know this might have a really simple answer but I can't seem to figure it out.

I need to define the contents of a variable depending on the value of a parameter coming to a function.

The issue is that when I set my values inside an if I get ReferenceError: newval is not defined

Below is a simplified version of the code which runs the error:

    const myMessage = (recipientId, msg, miller) => {
      const tip = 1;
      if (tip===1) 
        {
        console.log("in here");
        const newval = "some value";
        }
      console.log("executes this");
      const send = newval;

When I check the console I get the in here before the execute this message. That is why I am not sure why send doesn't know what newval is.

Any tip in the right direction will be appreciated, Thanks

9
  • 2
    const and let are block scoped, they are not hoisted like var Commented Jun 2, 2016 at 17:26
  • I wonder how people decide to use new language features without learning about them first... Commented Jun 2, 2016 at 17:33
  • @FelixKling I wonder what your definition on learning might be. For me, posting questions about my learning experience in order to seek an answer is learning :) Commented Jun 2, 2016 at 17:36
  • Primarily for me: reading documentation: "Constants are block-scoped, much like variables defined using the let statement.". Commented Jun 2, 2016 at 17:39
  • @FelixKling yes, with tomorrow newspaper I could do much more than arrive to that URL. The thing is that before asking I was not aware that const was the issue so there was no way for me to arrive to that part of the documentation. Maybe built better documentations, with findable user cases and I won't have to bother (?) you anymore :) Commented Jun 2, 2016 at 17:47

2 Answers 2

10

let and const are both sensibly block-scoped, unlike var, so you cannot reference them outside of the surrounding block (not function):

function scopes() {
  if (true) {
    var foo = 'visible';
    let bar = 'not visible';
    console.log(foo, bar); // both foo and bar are visible
  }
  console.log(foo, bar); // foo is visible, bar is not
}
console.log(foo, bar); // neither foo nor bar are visible

If you want to set const to the result of a branch, try moving the branch to a small function and call that:

function getNewValue(tip) {
  if (tip === 1) {
    // do stuff
    return "some value";
  } else {
    // do other stuff
    return "other value";
  }
}

const tip = 1;
const send = getNewValue(tip);
Sign up to request clarification or add additional context in comments.

Comments

1

Normally you declare let and const above the block ( if you need them outside the block )

Don't forget that const and let are block level and are also NOT hoisted. var is scope level and hoisted.

Here's a nice link about hoisting, if you're not familiar with it: http://javascriptissexy.com/javascript-variable-scope-and-hoisting-explained/

'use strict';
 const myMessage = (recipientId, msg, miller) => {
   const tip = 1;
   let newval;
   if (tip === 1) {
     console.log("in here");
     newval = "some value";
   }
   console.log("executes this");
   const send = newval;
 }
 myMessage()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.