JavaScript is a very interesting programming language.
It is easy and quick to code, runs immediately in runtimes like the web or Node.js, and is quite fast despite being a dynamic type language.
However, the freedom of JavaScript seems to be somewhat controversial.
For example, many developers believe that strict comparison is essential when comparing values.
Implicit Casting in JavaScript
I like to use implicit casting when writing JavaScript. All values in JavaScript can be implicitly cast to boolean.
True
1, -1 // non-zero number
" " // non-empty string
{}, [], function() {}, () => {}, this // non-null object
False
0 // zero
"" // empty string
null // null object
undefined // undefined
Loose Comparison in JavaScript
Loose comparison in JavaScript casts the compared values to the number type if they are of different types. Object types are compared by their address values, but the equality comparison of null and undefined is exceptionally treated as true.
123 >= "00123" // 123 >= 123
2 == true // 2 == 1
{} == {} // *{} == *{}
null == undefined // true
null >= undefined // *null >= undefined
Conclusion
JavaScript is a very liberal language, and the features of JavaScript such as implicit casting may seem confusing at first, but I personally think this is one of the advantages of JavaScript.
Thank you.
Top comments (3)
I think it really depends on the developer. A language “freedom” can be a blessing or a curse, depending on how you use it.
I personally learned how to use it to my advantage and I generally prefer dynamically typed languages. But I would recommend beginners to learn a strictly typed language at first (Java, for example).
I recommend JavaScript for beginners, but I think it is definitely easier to learn JavaScript if they already know Java.
I wouldn't recommend Javascript as a first language, it is very unique and a developer can develop bad habits by learning such a language as a first one.