OMFG, what the hell are you talking, son? Is this flame bait?
Right. If you open source code of almost any parser you would see that parser knows type of the value at the moment of parsing ("afsd" - string, 123 - number, etc.). By this definition JS is statically typed.
That's not it. Your examples are values, not variables. Please consider:
functionfoo(bar){console.log(bar)}
What's the type of bar? It could be string, number, object, etc. The interpreter will determine it during run time and emit the code to support it accordingly, so if you call foo("abc"), foo(1), and foo({}), you get three compiled code blocks that the interpreter determined during runtime that they where required.
In case you are still not satisfied, check this code:
If you call that code, during runtime the JS engine determines the type of the variable bar.
That's the definition of dynamic typing. Types of VARIABLES are not known/determined until runtime. That means that code can fail in runtime, not compile time, if a invalid type is provided.
If you would use reflection in Java you would be able to determine type at runtime. So Java is dynamically typed.
I may not have been clear, so: not you, the programmer. It's the compiler that doesn't know the type in compile time. Java is statically typed because all variables have type that are statically defined in compile time.
One more fundamental thing to note: variable type doesn't change in static typed languages, whereas they can change in dynamic typed languages. Check this code:
// Java/C/C#intnum=10;num="abc";// compiler error
However, this JS code is valid:
letnum=10;num="abc";
Not CS terms.
This is why I got mad at you. What makes you assert this? Take a look at the results in google:
This will give you a number of results of books on compiler design that talks about the weak typing and strong typing. I hope this convinces you, for your sake.
Good luck.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
OMFG, what the hell are you talking, son? Is this flame bait?
That's not it. Your examples are values, not variables. Please consider:
What's the type of
bar
? It could be string, number, object, etc. The interpreter will determine it during run time and emit the code to support it accordingly, so if you call foo("abc"), foo(1), and foo({}), you get three compiled code blocks that the interpreter determined during runtime that they where required.In case you are still not satisfied, check this code:
If you call that code, during runtime the JS engine determines the type of the variable bar.
That's the definition of dynamic typing. Types of VARIABLES are not known/determined until runtime. That means that code can fail in runtime, not compile time, if a invalid type is provided.
I may not have been clear, so: not you, the programmer. It's the compiler that doesn't know the type in compile time. Java is statically typed because all variables have type that are statically defined in compile time.
One more fundamental thing to note: variable type doesn't change in static typed languages, whereas they can change in dynamic typed languages. Check this code:
However, this JS code is valid:
This is why I got mad at you. What makes you assert this? Take a look at the results in google:
https://www.google.com/search?sxsrf=ACYBGNQ7UX3Dc2VF-TcwYMPvKWARNQg2YA%3A1574698748299&ei=_P7bXavmEcKP0AaPw4TgCw&q=weak+vs+strong+typing&oq=Weak+vs+strong+ty&gs_l=psy-ab.3.0.0i20i263j0i22i30l9.4213.5245..6276...0.2..0.106.313.0j3......0....1..gws-wiz.......0i71j0i67j0.3V2-LF1mLSI
Not convinced?
https://www.google.com/search?tbm=bks&q=weak+vs+strong+typing
This will give you a number of results of books on compiler design that talks about the weak typing and strong typing. I hope this convinces you, for your sake.
Good luck.