ES6 const is not immutable

1 xFJr7vK8l6adpUNe60AVJQ

Now most of the ES6 features can be used in modern browsers without transpiling. If you are using ES6 features in your project then you must have come across the const keyword.

Most of us know that the value assigned to the const cannot be changed. Yes, this is true when your are assigning primitive values.

const-2

But when comes with object type you can change the object’s property.

const-1

This proves that the const is not immutable and it cannot be reassigned or re-declared.

MDN says this clearly in their documentation.

The `const` declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

Happy Scripting…

Advertisements

Simple type casting in Javascript

This is my first blog for my very favorite language.

JavaScript is a very flexible language that would make to do many magical things. But the programming this language should be done careful since every browser is running in a single thread.

Type casting in JavaScript

Number

We all know the string or date can be converted to number using parseInt/parseFloat. But we can also use the “+” operator to do the same.

var d = +"43" //43

var d = +"4.3" //4.3

var d = +"4e" //NaN

var d = +new Date() //returns date value in milliseconds.

String

ToString method can be used to convert any number, boolean or date to string value. But it can be more simple by using the concatenating a “” at the end.

var d = 4 + "" //"4"

var d = new Date() + "" //"Wed Jul 22 2015 15:40:26 GMT+0530 (India Standard Time)"

String to Boolean

There is no simple way to cast from “true” string value to true boolean type. We have to do as follows.

var d = val == "true" ? true : val == "false" : false;