DEV Community

Elayaraj C
Elayaraj C

Posted on

What Is JavaScript and Function & Data Types Why Use Return Types?

🌐 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.

Image description

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
Enter fullscreen mode Exit fullscreen mode

βœ… 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;
}

Enter fullscreen mode Exit fullscreen mode

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!");
}

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

⚠ Downsides:

Can cause bugs because it’s hoisted (moved to the top of the function) and allows accidental re-declaration.
Enter fullscreen mode Exit fullscreen mode

🌟 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
Enter fullscreen mode Exit fullscreen mode

βœ” 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

Enter fullscreen mode Exit fullscreen mode

⚠ 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
Enter fullscreen mode Exit fullscreen mode

Image description

⚑ 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)