π What Is JavaScript?
JavaScript (often shortened as JS) is a high-level, interpreted programming language used primarily for adding interactivity and dynamic behavior to web pages.
Think about it like this:
- HTML gives the structure.
- CSS gives the style.
- JavaScript gives the behavior.
For example, when you click a button and something pops up, or when you fill out a form and it checks your input β thatβs JavaScript working behind the scenes.
** What Are Functions in JavaScript?**
A function in JavaScript is a reusable block of code designed to perform a specific task.
Hereβs a basic example:
function greet(name) {
return "Hello, " + name + "!";
}
In this example:
- greet is the function name.
- name is a parameter.
- return gives back a value when the function is called.
Functions help organize code, avoid repetition, and make programs easier to maintain.
When a function returns something, it gives back one of these data types.
π Why Use Return Types in Functions?
You might wonder β why should we bother using a return statement and thinking about the return type?
Hereβs why:
β
Reusable Outputs
With a return, you can reuse the result somewhere else in your code.
function add(a, b) {
return a + b;
}
let sum = add(5, 3); // sum is now 8
β
Predictable Behavior
Knowing the return type (like Number, String, or Boolean) helps you predict how the function behaves and how you can use the result.
β
Modularity
Functions that return values can be chained together or combined, making your code modular and clean.
β
Avoid Side Effects
Functions that only work on inputs and return outputs without changing anything outside (called pure functions) are easier to test and debug.
β‘ Example: Different Return Types
// Returns a Number
function multiply(a, b) {
return a * b;
}
// Returns a String
function getWelcomeMessage(name) {
return "Welcome, " + name + "!";
}
// Returns a Boolean
function isEven(number) {
return number % 2 === 0;
}
By knowing the return type, you know exactly how to handle the result.
π When to Use Return, When Not To?
- Use return when you need to pass back a value.
- Skip return (or return void) when you only need to perform an action (like logging to the console or updating the UI).
Example (no return):
function showAlert() {
alert("This is a message!");
}
Types of Variables in JavaScript
π 1οΈβ£ var
β
Oldest way (before ES6)
β
Function-scoped β the variable is visible inside the function where itβs declared
β
Allows re-declaration and reassignment
Example:
var name = "John";
var name = "Doe"; // This works
name = "Smith"; // This works too
β Downsides:
Can cause bugs because itβs hoisted (moved to the top of the function) and allows accidental re-declaration.
π 2οΈβ£ let
β
Introduced in ES6
β
Block-scoped β visible only within the block {} where itβs declared
β
Allows reassignment, but no re-declaration in the same scope
Example:
let age = 25;
// let age = 30; β Error: already declared
age = 30; // β
This works
β Better than var for most cases because itβs more predictable.
π 3οΈβ£ const
β
Also introduced in ES6
β
Block-scoped like let
β
Cannot be reassigned β constant value
β
Must be initialized when declared
Example:
const PI = 3.14159;
// PI = 3.14; β Error: can't reassign
β Important:
If the const holds an object or array, the reference canβt change, but the contents can be modified.
const user = { name: "Alice" };
user.name = "Bob"; // β
This works
β‘ When to Use What?
β Use const by default β itβs safer and shows intent that the variable wonβt change.
β Use let when you know the value will change (like in loops or counters).
β Avoid var unless you have a specific reason β modern best practices discourage its use.
Top comments (0)