Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
10 Interesting things you can do with a few lines of JavaScript
In this tutorial, let us discuss some fascinating things that a few lines of JavaScript can do.
Almost every website uses JavaScript. JavaScript was born in 1995, and it is a programming language similar to other languages, but it runs faster because it doesn't have many predefined functions.
JavaScript can create programs, libraries, and scripts for websites or desktop applications. A lot of web developers are also good at writing JavaScript. Some JavaScript codes are fascinating if you observe how it behaves. Let us discuss what are these fascinating things in JavaScript.
1. Fascinating Things About Semicolons
Unlike other languages, JavaScript has automatic semicolon insertion (ASI). While it's recommended to use semicolons, JavaScript can still run without them in many cases. However, starting code with a semicolon is not a best practice:
// This works but is not recommended ;var x = 5; console.log(x);
5
2. Interesting Things About NaN
In JavaScript, NaN (Not a Number) is ironically of type "number". Even more interesting, NaN is not equal to itself:
console.log(typeof NaN); // "number" console.log(NaN === NaN); // false console.log(NaN == NaN); // false console.log(isNaN(NaN)); // true
number false false true
3. Interesting Things About Null
A null represents an intentional absence of value, but typeof null returns "object" - this is a known JavaScript quirk:
console.log(typeof null); // "object" (known bug) console.log(null === null); // true console.log(null == undefined); // true console.log(null === undefined);// false
object true true false
4. Interesting Things About Functions
Functions can run themselves immediately using IIFE (Immediately Invoked Function Expression). This pattern is useful for creating isolated scopes:
// IIFE - runs immediately
(function(){
console.log('hi');
})();
// Another IIFE syntax
(function(name){
console.log('Hello ' + name);
})('World');
hi Hello World
5. Interesting Things About Script Tags
When a script tag has a src attribute, any code inside the tag is ignored. You cannot combine external scripts with inline code:
<!-- This won't work as expected -->
<script src="external.js">
console.log("This code will be ignored");
</script>
<!-- Correct approach -->
<script src="external.js"></script>
<script>
console.log("This code will run");
</script>
6. Fascinating Things About Type Coercion
JavaScript's automatic type conversion can produce surprising results when mixing strings and numbers:
console.log("100" + 2); // "1002" (string concatenation)
console.log(100 + "2"); // "1002" (string concatenation)
console.log("4" / "2"); // 2 (numeric division)
console.log(2 + "3"); // "23" (string concatenation)
console.log("4" + 5); // "45" (string concatenation)
console.log("8" + 3 + 7); // "837" (left to right)
console.log(3 + 1 + "5"); // "45" (addition first, then concatenation)
1002 1002 2 23 45 837 45
7. Interesting Things About IIFE Syntax
The way you define IIFE affects whether it runs or throws an error:
// This works - assignment context
var test1 = function () {
console.log("IIFE assignment works");
}();
// This would cause syntax error if not wrapped
// function test2() {
// console.log("This won't work");
// }();
// This works - wrapped in parentheses
(function test3() {
console.log("IIFE with parentheses works");
})();
IIFE assignment works IIFE with parentheses works
8. Interesting Things About Undefined
The undefined value can be reassigned in older JavaScript versions, leading to confusing behavior:
var vb; console.log(vb == undefined); // true console.log(typeof vb); // "undefined" // In modern JavaScript, undefined cannot be reassigned // But in older versions, this was possible and dangerous console.log(undefined === void 0); // true (safe way to check)
true undefined true
9. Interesting Things About Parentheses and Return
The position of opening braces affects automatic semicolon insertion:
function a() {
return
{
x: '1'
}
}
function b() {
return {
x: '1'
}
}
console.log(a()); // undefined (ASI adds semicolon after return)
console.log(b()); // { x: '1' }
console.log(typeof a() === typeof b()); // false
undefined
{ x: '1' }
false
10. Interesting Things About Numbers
JavaScript has some quirky number behaviors due to floating-point precision limits:
console.log(9999999999999999); // 10000000000000000 (precision loss) console.log(0.1 + 0.2); // 0.30000000000000004 console.log(0.1 + 0.2 === 0.3); // false let x = -0; let y = 0; console.log(x === y); // true console.log(1/x === 1/y); // false (-Infinity vs Infinity)
10000000000000000 0.30000000000000004 false true false
Conclusion
These JavaScript quirks demonstrate the language's flexible but sometimes unpredictable nature. Understanding these behaviors helps developers write more reliable code and debug unexpected issues. While some are historical bugs that remain for compatibility, they're part of what makes JavaScript unique.
