0

Just read that (const) Behavior will vary across browser implementations. The answer I pointed to is pretty old (2012) so, is it still so bad?

As I need cross browser compatibility in my project so should I avoid use of const keyword?

If 'yes' - how do I replace one?

UPD:

Just checked the description on the developer.mozilla.org. And YES: looks like it is that bad...

enter image description here

Community
  • 1
  • 1
Vlada Katlinskaya
  • 991
  • 1
  • 10
  • 26
  • this is a question related to ES6 const and porting those references to browsers that don't support that. Therefore, you'll need a transpiler (`babel`) to ensure ES5 browsers like IE 11 (and below) can run your js. – Denis Tsoi Feb 16 '17 at 09:10
  • @siam As I understand variable defined with `const` has some protection. So I'd say that it would be a good idea to use ones... If it is not possible - I don't see any serious reasons to avoid `var`. Could you post this as an answer to let me accept one? Thanks! – Vlada Katlinskaya Feb 16 '17 at 09:17
  • @siam `var` is not equivalent to `let` or `const`. – Madara's Ghost Feb 16 '17 at 09:20
  • @MadaraUchiha did I say its equivalent? :/ – m87 Feb 16 '17 at 09:21

2 Answers2

1

const isn't available in old browsers (I'm looking at you, IE, Safari).

If you want to play safe, and you have no build step (Babel, TypeScript, etc), use only var. Both Babel and TypeScript will compile your consts and lets to vars. So if you use any of those tools, you can use const and let freely.

Be mindful that var is not strictly equivalent to either let or const. (var is hoisted, and has a different scope).

Also, if you don't care about IE or Safari, you can pretty much use const without a build tool.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

You can just simply use var instead of const to support old browsers. (** remember var isn't equivalent to const to some extent) and to protect a var from being changed you can just put it inside a private scope instead of global. (so it'll work partly like a const)

m87
  • 4,445
  • 3
  • 16
  • 31