Javascript: changing the value type of a variable

1

Reading about JavaScript I found this example with two notes:

var message = " hi ";
message = 100;

1) We declare message as String and then overwrite it as a variable of type integer , it is valid but NOT recommended.

2) This is supported by ECMAScript .

  

Why is it not recommended to change the value type of the variable in this way?
  In what way would it be more convenient?

    
asked by Victor Alvarado 29.04.2017 в 05:42
source

2 answers

1

Let's see, let's go in parts:

In Javascript, as in many other interpreted languages, variables do not have type ; They have values , which is something else.

A variable is a totally neutral element : you can assign it the value you want. The type of the variable is determined, at all times, by the type of the value that it contains: if the variable does not exist or has no value assigned, then its type is undefined , and, from there, it goes adopting the type of value that we assign: number , string , object , ...

At any time, we can use typeof to see the type of the value assigned to a variable.

Why it is not convenient to assign values of different types to the same variable is another issue: it can be considered an adaptation of the principle of sole responsibility : use a variable to one, and only one, function or responsibility.

This principle greatly reduces the number of errors in the code: if we call a variable, say, HoraDeInicio , and decide that its value will be a Date object with the start time of a certain operation, < strong> we should not use it to, for example, contain the string with the text representation of that date / time.

Thus, at any point in our code where we see HoraDeInicio , we know what it is and what it contains; whereas if we see StringHoraDeInicio , we deduct quickly that it contains a string with the text representation of the start time of something.

If you use the first one for both, not you can know at a glance the value it contains at any point in your code; you should look backwards , looking for the last assignment, to know the value it contains and if you can use it or not for a certain operation.

    
answered by 29.04.2017 / 06:52
source
3

It's really just a matter of concepts:

  

Dynamic typing: A variable can take different types of data at any point. You can start with string and then move to number, etc. This type of typing is implemented by interpreted (and partially) languages such as Python, Ruby, JavaScript.

     

Static typing: Type checking is done in compilation and not in execution, such as dynamic, which ensures a variable has only one type during its cycle. lifetime. This type of typing is implemented by languages such as Java, C #, Haskell, etc.

There are many discussions on What is better, dynamic typing or static typing? , but in the end they do not reach any point. Each approach has its own advantages and disadvantages. I recommend reading this answer in SO in English.

In general terms, a dynamic typing allows you to be more flexible, since you do not worry about types in the first instance, relying on the quality of the code and testing to ensure that there will be no unexpected results. On the other hand, with static typing you tend to believe that you "ensure" consistency between types, but this is not totally true, since, in runtime, unexpected cases can occur that can not be covered in compilation time.

Going directly to the query language, JavaScript, you can not have a static typing because the language design intervenes; However, what you can do is define types in JavaScript and track them as you develop, this through Flow , a great tool for Facebook. Flow has support for most editors and IDEs.

JavaScript example using Flow:

async function findTweetsOf(id: String): Promise<Tweet> {
  let tweets: Array<Tweet> = await TwitterClient.findByUser(id);
  return tweets;
}

If you call that function with a value other than String, what you will get is an error and the line will mark you in the editor. Flow works very well with ESLint .

    
answered by 29.04.2017 в 14:59