JavaScript has some weirdness that you should be aware of. This section will cover some of the most common ones. This is not an exhaustive list, but it should give you a good idea of what to look out for.
Note: This doesn't mean JavaScript is bad. It's just different from other languages, and it has its own quirks (just like any other language).
In JavaScript, the this
keyword behaves differently than in other languages. It's a common source of confusion for beginners.
In most languages, this
refers to the current instance of the class. In JavaScript, this
refers to the object that called the function.
const person = {
name: 'John',
sayHello: function() {
console.log('Hello, ' + this.name);
}
};
person.sayHello(); // Hello, John
In the example above, this
refers to the person
object. If we were to call person.sayHello
from another object, this
would refer to that object instead. This is somewhat similar to Python's self
parameter in methods.
In JavaScript, there are two equality operators: ==
and ===
. The ==
operator checks for equality after doing type conversion, while the ===
operator checks for equality without type conversion.
console.log(5 == '5'); // true
console.log(5 === '5'); // false
In the example above, 5 == '5'
is true because JavaScript converts the string to a number before comparing. 5 === '5'
is false because it doesn't do type conversion.
It's generally a good idea to use ===
to avoid unexpected behavior.
In JavaScript, null
and undefined
are similar but not the same. null
is an explicit value that means "no value", while undefined
means that a variable has been declared but hasn't been assigned a value.
let x;
console.log(x); // undefined
let y = null;
console.log(y); // null
In the example above, x
is undefined
because it hasn't been assigned a value, while y
is null
because we explicitly set it to null
.
JavaScript is known for its type coercion, which can lead to unexpected behavior if you're not careful. Some operations just work, even if the types don't match, because JavaScript tries to convert the types for you. This is both good and bad, depending on whether you're aware of it.
console.log(5 + '5'); // 55
console.log(5 - '5'); // 0
In the example above, JavaScript converts the number 5
to a string in the first case, and it converts the string '5'
to a number in the second case. This can lead to unexpected results if you're not careful. It's generally a good idea to be explicit about types to avoid confusion, for example:
console.log(5 + Number('5')); // 10
By using Number('5')
, we're explicitly converting the string (or any type that might allow it) to a number, which makes the code easier to understand.