JavaScript has typed values, not typed variables, what does this mean?

JavaScript has typed values, not typed variables, what does this mean?
0

#1

I am reading book You don’t know js, up and going.

And at chapter 2, into javascript, the author said :

JavaScript has typed values, not typed variables.

What does this mean?? Could someone who has a deeper knowledge of js explain this?


#2

Well, in some other languages such as c. When you declare a variable you declare a type instead of the catch all var or let. Example: string s <-this is a variable that will hold a string. JavaScript uses var or let for basically any declaration but still the values in variables have types. As an example var a=97 is a variable has a value type of number, var a=“hello world” has a value type of string


#3

This is inn contrast to what we call “strongly typed languages”. In some languages, you have to declare variables as a certain type.

String mystr = "I'm a string";
int mynum = 42; 

In JavaScript, we don’t do this.

var sluttyVar = "stringy"; // the value we assigned to sluttyVar is a string
sluttyVar = 99; // now the value assigned to sluttyVar is a number

That’s a very broad answer. To go deeper into this, read up on “strongly typed” vs “weakly typed” languages.


#4

I see. I saw something like this on the next paragraph of the book. I think this explains it:

Notice how in this snippet the a variable holds every different type of value, and that despite appearances, typeof a is not asking for the “type of a”, but rather for the “type of the value currently in a.” Only values have types in JavaScript; variables are just simple containers for those values.


#5

I read somewhere in the book that says :

js is a weakly typed language, the value type of the same variable is interchangeable.

Is this correct? Like the example you just gave out, sometimes var a is a string, sometimes its a number.


#6

Yup. Some people who get defensive about how “weakly typed” sounds will say “dynamically typed” instead. :laughing:


#7

okok stop confusing me with all these names…

also nice naming method on that example you gave out :wink:

var sluttyVar Gonna implement this into my coding style. :slight_smile:


#8

FCC tutoring: I don’t do it for the money; I don’t do it for the fame; I do it for the LOLZ.


#9

lol.

Here. take it. :stuck_out_tongue: