Home > Web Front-end > JS Tutorial > razy Things In JavaScript: Love It or Hate It

razy Things In JavaScript: Love It or Hate It

DDD
Release: 2025-01-03 06:55:39
Original
117 people have browsed it

JavaScript is the language we all love… and sometimes want to throw our keyboards at. It’s everywhere! From making your buttons dance to breaking your website at 2 AM for reasons no one can explain.

JavaScript is powerful, quirky, and downright weird. Let’s talk about some of the craziest things it does.

1. “NaN” Is a Number?

You read that right. “NaN” stands for “Not a Number,” yet JavaScript classifies it as a number. It’s like someone saying, “I’m not hungry… but let’s go eat.”

console.log(typeof NaN); // "number"
Copy after login

Why, JavaScript? Why?

2. Adding Arrays? Sure, Why Not.

What happens when you add two arrays? You’d think JavaScript would throw an error, right? Nope. It just… joins them into a string.

console.log([1, 2] + [3, 4]); // "1,23,4"
Copy after login

This isn’t addition; this is nonsense. But hey, that’s JavaScript for you.

3. True True = 2?

Try this in your console:

console.log(true + true); // 2
Copy after login

Yep. Because true is treated as 1 and JavaScript thinks, "Math makes sense here!" It doesn’t, but let’s pretend it does.

4. The Mysterious undefined and null

undefined means something hasn’t been assigned a value. null means it’s empty.
But are they the same? No.

console.log(undefined == null); // true
console.log(undefined === null); // false
Copy after login

Confused? So was I. And so is every new JavaScript developer.

5. The this Problem

Ah, this. The bane of JavaScript learners. In one context, this is an object. In another, it’s undefined. In an arrow function? It’s something else entirely.

const obj = {
  name: "JavaScript",
  regular: function () {
    console.log(this.name);
  },
  arrow: () => {
    console.log(this.name);
  },
};

obj.regular(); // "JavaScript"
obj.arrow();   // 
Copy after login

Every time you think you understand this, JavaScript pulls the rug out from under you.

6. Double Equals Is Lazy

In JavaScript, == doesn’t always care about type. So, it tries to convert things for you. That’s nice... until it isn’t.

console.log(0 == "0"); // true
console.log(0 == []); // true
console.log([] == ""); // true
Copy after login

Do yourself a favor: use === instead. Always.

7. Infinite Is a Number

What’s the biggest number in JavaScript? Infinity. What’s smaller than the smallest? Negative infinity. And yes, you can do math with them.

console.log(Infinity - Infinity); // NaN
console.log(Infinity > 1000000);  // true
Copy after login

JavaScript is just casually flexing that math is relative.

razy Things In JavaScript: Love It or Hate It

How to Check if a Key Exists in an Object in JavaScript | Tajammal Maqbool

Have you ever been knee-deep in JavaScript code and wondered, How to check if a key exists? In this blog, 4 different methods are explained.

razy Things In JavaScript: Love It or Hate It

Time Events in JavaScript: A Guide | Tajammal Maqbool

JavaScript is amazing, isn’t it? It makes websites interactive and alive. One of its coolest features is Time Events. Don’t worry if it sounds technical; I’ll break it down.

razy Things In JavaScript: Love It or Hate It

JavaScript Splice - The Ultimate Array Method | Tajammal Maqbool

Master JavaScript's powerful splice method! Learn how to add, remove, and replace array elements effortlessly. Simplify your code with this ultimate guide.

razy Things In JavaScript: Love It or Hate It tajammalmaqbool.com

Why We Love It Anyway

For all its quirks, JavaScript is… amazing. It lets you build entire applications, make websites interactive, and even control robots! It’s a little crazy, but that’s part of its charm.

JavaScript teaches us patience, makes us laugh (and cry), and, in the end, gets the job done. Embrace the weirdness.

The above is the detailed content of razy Things In JavaScript: Love It or Hate It. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template