Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, December 20, 2015

Const in javascript? When to use it and is it necessary

Const in javascript? When to use it and is it necessary


I've recently come across the const keyword in javascript. From what i can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in node.js):

const x = 'const';  const x = 'not-const';    // Will give an error: 'constant 'x' has already been defined'  

I realise that it is not yet standardised across all browsers - but I'm only interested in the context of node.js/v8, and I've noticed that certain developers / projects seem to favour it heavily when the var keyword could be used to the same effect.

Questions?

When is it appropriate to use const in place of var?

Should it be used every time a variable which is not going to be re-assigned is declared?

Does it actually make any difference if var is used in place of const or vice-versa?

Answer by Carl Markham for Const in javascript? When to use it and is it necessary


Personal preference really. You could use const when, as you say, it will not be re-assigned and is constant. For example if you wanted to assign your birthday. Your birthday never changes so you could use it as a constant. But your age does change so that could be a variable.

Answer by Funkotron_King for Const in javascript? When to use it and is it necessary


In my experience I use const when I want to set something I may want to change later without having to hunt through the code looking for bits that have been hard coded e.g. A file path or server name.

The error in your testing is another thing though, you are tring to make another variable called x, this would be a more accurate test.

const x = 'const';  x = 'not-const';  

Answer by James Donnelly for Const in javascript? When to use it and is it necessary


Despite having fairly decent browser support, I'd avoid using it for now. From MDN's article on const:

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11. The const keyword currently declares the constant in the function scope (like variables declared with var).

It then goes on to say:

const is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the let statement, constants declared with const will be block-scoped.

If you do use const you're going to have to add in a workaround to support slightly older browsers.

Answer by Tibos for Const in javascript? When to use it and is it necessary


There are two aspects to your questions: what are the technical aspects of using const instead of var and what are the human-related aspects of doing so.

The technical difference is significant. In compiled languages, a constant will be replaced at compile-time and its use will allow for other optimizations like dead code removal to further increase the runtime efficiency of the code. Recent (loosely used term) JavaScript engines actually compile JS code to get better performance, so using the const keyword would inform them that the optimizations described above are possible and should be done. This results in better performance.

The human-related aspect is about the semantics of the keyword. A variable is a data structure that contains information that is expected to change. A constant is a data structure that contains information that will never change. If there is room for error, var should always be used. However, not all information that never changes in the lifetime of a program needs to be declared with const. If under different circumstances the information should change, use var to indicate that, even if the actual change doesn't appear in your code.

Answer by Edgar Gri?ant for Const in javascript? When to use it and is it necessary


You have great answers, but let's keep it simple.

const should be used when you have a defined constant (read as: it won't change during your program execution).

For example:

const pi = 3.1415926535  

If you think that it is something that may be changed on later execution then use a var.

The practical difference, based on the example, is that with const you will always asume that pi will be 3.14[...], it's a fact.

If you define it as a var, it might be 3.14[...] or not.

For a more technical answer @Tibos is academically right.

Answer by janesconference for Const in javascript? When to use it and is it necessary


To integrate the previous answers, there's an obvious advantage in declaring immutable variables, apart from the performance reason: if you accidentally try tho change or re-declare them in the code, the program will respectively not change the value or throw an error.

For example, compare:

// Will output 'SECRET'    const x = 'SECRET'  if (x = 'ANOTHER_SECRET') {  // Warning! assigning a value variable in an if condition      console.log (x)  }  

with:

// Will output 'ANOTHER_SECRET'    var y = 'SECRET'  if (y = 'ANOTHER_SECRET') {       console.log (y)  }  

or

// Will throw TypeError: const 'x' has already been declared    const x = "SECRET"    /*  complex code */    var x = 0  

with

// Will reassign y and cause trouble    var y = "SECRET"    /*  complex code */    var y = 0  

Answer by Genovo for Const in javascript? When to use it and is it necessary


It provides: 1) a constant reference, eg const x = [] - the array can be modified, but x can't point to another array; and 2) block scoping. const and let will together replace var in ecma6/2015 See discussion at https://strongloop.com/strongblog/es6-variable-declarations/


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 71

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.