Open In App

Interesting Facts About JavaScript

Last Updated : 23 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

JavaScript (often abbreviated as JS) is one of the most popular programming languages in the world. It is an interpreted, high-level programming language that follows ECMAScript. It powers interactive websites and is packed with amazing features that make it special and powerful.

Interesting Facts About JavaScript

Here are some interesting facts about JavaScript

  • JavaScript Was Created in Just 10 Days: JavaScript was developed by Brendan Eich in 1995 for Netscape. He completed the first version in a whopping 10 days to meet the pressing needs of the browser market!
  • JavaScript Is Not the Same as Java: Despite the similar names, JavaScript and Java are two entirely different languages. Java is a statically typed, object-oriented language, while JavaScript is dynamically typed and versatile in both functional and object-oriented programming.
  • Interpreted Language: JavaScript is interpreted, not compiled, which means it runs line-by-line in the browser. This enables real-time feedback and rapid development.
  • Dynamic Typing with Structure: In JS variables don’t need an explicit type definition when declared, unlike strict languages such as C or C++. However, it offers more structure than languages like Python with the addition of let, const, and var keywords for variable declarations.
  • The Foundation of Modern Web: Most web applications today rely on JavaScript to provide interactive, dynamic content, making it a core pillar of the web alongside HTML and CSS.
  • Runs Everywhere: Initially designed for client-side scripting, JavaScript now powers both client and server-side applications with the rise of technologies like Node.js.

JavaScript and Browsers

  • JavaScript Engines: Each major browser has its own JavaScript engine for running code. For instance
    • Google Chrome uses V8.
    • Firefox uses SpiderMonkey.
    • Safari uses JavaScriptCore.
    • Edge uses Chakra.
  • Asynchronous Capabilities: JavaScript is single-threaded, meaning it can execute one line of code at a time. However, its asynchronous capabilities (via callbacks, promises, and async/await) enable non-blocking code execution, making it ideal for modern applications.

Fun Features in JavaScript

Numbers: 64-Bit Floating Point by Default

In JavaScript, all numbers (integers and floating-point values) are stored as 64-bit floating-point numbers based on the IEEE 754 standard. Unlike C++ or Java, there are no distinct int, float, or double types.

Bitwise Operations: JavaScript performs bitwise operations on 32-bit signed integers. So to do the operation it needs to convert 64 bit numbers.

  • Converts the 64-bit floating-point number to a 32-bit binary number.
  • Executes the operation.
  • Converts the result back to a 64-bit floating-point number.
JavaScript
let n = 5.5;           // Stored as a 64-bit floating-point number
let res = n | 0;    // Bitwise OR operation
console.log(res);     // Output: 5

Output
5

Logical Operators Work with Numbers

JavaScript’s logical operators (&&, ||, !) work with numbers and other types, not just booleans, treating falsy and truthy values as logical states.

  • Falsy Values: false, 0, -0, “” (empty string), null, undefined, NaN, and document.all are considered falsy.
  • Truthy Values: Any value not listed above, including non-zero numbers and non-empty strings, is considered truth.
JavaScript
console.log(0 && 5);
console.log(5 && 10);
console.log(5 || 0); 

Output
0
10
5

Everything Is an Object (Sort of)

In JavaScript, Functions are objectsarrays are objects, and even primitive values can behave like objects temporarily when you try to access properties on them.

JavaScript
let s = "hello";
console.log(s.length);  

// Example with a number
let x = 42;
console.log(x.toString()); 

// Example with a boolean
let y = true;
console.log(y.toString());

/* Internal Working of primitives
   to be treeated as objects
   
// Temporary wrapper object
let temp = new String("hello"); 

console.log(temp.length); // 5

// The wrapper is discarded after use
temp = null; */

Output
5
42
true

Arrays Are Objects

JavaScript arrays are technically objects, which allows them to have non-integer keys.

JavaScript
const a = [1, 2, 3];
a["key"] = "value";
console.log(a.key); 

Output
value

In most languages, arrays are strictly defined structures and don’t allow such behavior.

Type Coercion

JavaScript has a unique way of handling types, often resulting in unexpected results

JavaScript
console.log(1 + "2"); 
console.log("5" - 2);

Output

"12"
3

This automatic conversion between types is called type coercion.

Hoisting

JavaScript moves declarations to the top of their scope before executing the code called hoisting. However, only the declaration is hoisted, not the initialization.

JavaScript
var greeting; // Declaration is hoisted to the top
console.log(greeting); // At this point, `greeting` is undefined
greeting = "Hello, World!"; // Initialization happens here
console.log(greeting); // Now, it logs "Hello, World!"

Output
undefined
Hello, World!

In Python or C++, referencing a variable before its declaration results in an error.

NaN Is a Number

In JavaScript, NaN (Not-a-Number) is actually of type number. It’s used to represent an invalid number, which can be confusing for beginners.

JavaScript
console.log(typeof NaN);
console.log(NaN === NaN); 

Output
number
false

A character is also a string

There is no separate type for characters. A single character is also a string.

JavaScript
let s1 = "gfg";   // String
let s2 = 'g';    // Character

console.log(typeof s1); 
console.log(typeof s2);

Output
string
string

Equality Quirks

JavaScript has two equality operators:

  • Double equals (==) performs type conversion before comparing.
  • Triple equals (===) checks both type and value without type conversion.
JavaScript
console.log(0 == "0");
console.log(0 === "0");

Output

true
false

Variables Without var

Declaring variables without var, let, or const automatically places them in the global scope. This can lead to unexpected behavior

JavaScript
function variable() {
    n = 10;
}
variable();
console.log(n); 

Output

10

But ‘n’ is now a global variable.

Related Topics



Next Article

Similar Reads