10 Things You Didn’t Know About JavaScript (most likely)
Learning JavaScript is like swimming on the ocean. One may be in the development field with JavaScript for many years but still there is something he may not know! Today I will share some of them that you may not know.
- JavaScript was originally going to be called LiveScript, but it was renamed in an ill-fated marketing decision that attempted to capitalize on the popularity of Java language — despite the two having very little in common. This has been a source of confusion ever since.
2. NaN
is not equal to NaN
!
NaN === NaN // false
3. You can define Object keys from variable directly:
Cont firstProperty = “name”;Const myObj = {[firstProperty]: “Hasan”,}
4. The length property of Array is not always the number of items the array contains.
For instance:
Remember, the length of an array is one more than the highest index.
5. If you use expressions in switch statements it won’t convert implicitly. It will act like ===
operator.
6. Empty string(“”) is treated as falsy value but empty array([]) and empty object({}) are treated as truthy value.
Boolean("") // falseBoolean([]) // true
Consider this:
Const a = {} || 5; // a={}Const a = “” || 5; // a=5
If you want to use them as truthy value then use array.length or object.property
Boolean([].length) // falseBoolean({}[“name”]) // false
7. String started with “0x” are treated as hexadecimal number.
parseInt(“F”) // NaNparseInt(“0xF”) // 15
8. You can write any character of any language using the escape sequence. Write "\u{XXXXXX}"
where XXXX is 1–6 hex digits in the range 0
–10FFFF
. e.g. “\u{0985} is “অ”. See the Unicode chart for all the character values.
9. Random function of Math object returns floating point between 0 to 1. But use can get any random number between any two integers using this formula:
function randomBetweenTwo(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
Though random is not so random!
10. You can create custom properties for Arrays.
const array = [1, 2, 3, 4, 5];
array.type = "numbers";
console.log(array.type); // numbers
That’s it for today. Keep exploring JavaScript.
Peace!