0

In most languages I would write

declare var1 = undefined
if (condition) {
 var1 = value1
}

but in javascript, it seems its allowed to write

if (condition) {
  let var1 = value1
} else {
  var1 = value2
}

Am I misguided on this?

eugene
  • 39,839
  • 68
  • 255
  • 489
  • 2
    `it seems its allowed to write` Not allowed if you're using `let` - it has block scope (along with `const`. are you sure you're not thinking of `var`?) – CertainPerformance Dec 16 '18 at 08:16
  • I would recommend just declaring the variable without setting it before all the conditions (ie, "let var;"), and setting it if you need to. One more line of code, but no issues and better readability imo. – Jim Dec 17 '18 at 15:49

4 Answers4

3

Yes you can when you use var.

Not when you use let and const because they are block scoped.

Example for var

if (true) {
 var var1 = 1
}
console.log(var1);

Example using let

if (true) {
 let var1 = 1;
}

console.log(var1)

P.S :- In JS world using var is considered as bad coding practice.You should avoid using it until there is something you can't do with let and const.

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • As I figure out from many resources including the Kyle Simpson's book YDKJS: "Programmers should give a priority to use var, let, cost in that order specifically. let is not the new var, we still have to use var where variables need to be there for the whole scope of the code" – Anas Abu Farraj Dec 16 '18 at 08:37
  • @Shakespear well that you can do by defining the `let` in outermost scope. – Code Maniac Dec 16 '18 at 08:39
  • Of course, we can define the let in outermost scope., but still... I think let shines in for loops and so, but we don't always have to replace var with let keyword as a new habit. – Anas Abu Farraj Dec 16 '18 at 08:46
1

In JavaScript, a variable can be declared after it has been used. In other words: a variable can be used before it has been declared. Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

So:

x = 5
// ... some other code ...
var x

will be translated to:

var x;    
x = 5
// ... some other code

But it would only work this way if you used var to declare variable. If you use const or let it wouldn't work, because variables and constants declared with let or const are not hoisted.

Declaring variables using let or const is preferred way in modern javascript.

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
1

No, it's completely bad idea. If you want to use JavaScript variable both inside and outside of the if statement you can do it by declaring it outside the if statement.

let var1 = value1;
if (condition) {
  var1 = value1
} else {
  var1 = value2
}

This way you will only create a block scoped variable. But, if you use var var1 = value1 then it will declare globally scoped variable which is not what you wanted most probably. To learn more about how variables works in javascript, you can checkout this awesome article. Happy coding :)

bjupreti
  • 353
  • 3
  • 6
1

You can do this using let, but NOT in Strict Mode!

'use strict';
var condition = false;

if (condition) {
  let var1 = 42;
  console.log(var1);
} else {
  var1 = 43;
  console.log(var1);
}
// ReferenceError: var1 is not defined

It's recommended to declare var1 outside the scope of if statement:

'use strict';
var condition = false;
var var1 = 42;

if (condition) {
  console.log(var1);
} else {
  var1 = 43;
  console.log(var1);
}
// => 43
Anas Abu Farraj
  • 1,540
  • 4
  • 23
  • 31