Properties
JavaScript is a powerful and flexible language, but what exactly makes it tick? Here are the core properties that define how it works and why it’s so popular.
Interpreted Language
JavaScript is interpreted, not compiled (although modern engines do some compiling under the hood for performance).
- Code is executed line by line.
- No separate compilation step.
- Works directly in browsers or Node.js environments.
Dynamically Typed
Variables in JavaScript don’t have explicit types.
let message = "Hello"; // String
message = 42; // Now a Number- Flexibility comes at the cost of potential type-related bugs.
- Use tools like TypeScript for stricter type enforcement.
Prototype-Based Inheritance
Unlike classical inheritance (classes and objects), JavaScript uses prototypes.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log(`Hi, I'm ${this.name}`);
};- Objects inherit directly from other objects.
- Provides flexibility but can be confusing to newcomers.
Single-Threaded & Event-Driven
JavaScript runs in a single-threaded environment but handles asynchronous tasks via the event loop.
- No parallel code execution by default.
- Uses callbacks, Promises, and
async/awaitfor asynchronous programming.
First-Class Functions
Functions are first-class citizens:
- Can be assigned to variables
- Passed as arguments
- Returned from other functions
function greet(name) {
return `Hello, ${name}`;
}
const greeter = greet;
console.log(greeter("Alice")); // "Hello, Alice"Loosely Typed Variables
Variables are declared with var, let, or const and do not require a type declaration.
let count = 10; // Number
count = "Ten"; // String- Easier to write but easier to mess up.
Runs in Browser and Server
- In browsers: Controls the DOM, listens to events, manipulates UI.
- On server (with Node.js): Handles HTTP requests, connects to databases, performs file system operations.
JSON Native
JavaScript Object Notation (JSON) is JavaScript’s native data format.
const user = {
name: "Alice",
age: 30
};
const jsonString = JSON.stringify(user);
const parsedUser = JSON.parse(jsonString);- Makes data interchange between server and client extremely easy.
Asynchronous Nature
With Promises and async/await, handling asynchronous tasks becomes clean and readable:
async function fetchData() {
const response = await fetch("https://api.example.com/data");
const data = await response.json();
return data;
}