Function, Objects and Array In JS.

Published: (December 25, 2025 at 02:00 AM EST)
1 min read
Source: Dev.to

Source: Dev.to

Functions

A function is a block of code that performs a specific task and can be reused. There are three ways to define a function in JavaScript:

  1. Function Declaration
  2. Function Expression
  3. Arrow Function (ES6) – short syntax, commonly used in modern JS.

Examples

// Function Declaration
function greet(name) {
  return "Hello " + name;
}

console.log(greet("Ajmal"));
// Function Expression
const add = function (a, b) {
  return a + b;
};

console.log(add(5, 3));
// Arrow Function
const multiply = (a, b) => a * b;

console.log(multiply(4, 2));

Objects

Objects store data in key‑value pairs and combine state with behavior.

const person = {
  firstName: "John", // Property
  lastName: "Doe",   // Property
  age: 50,           // Property
  fullName: function() { // Method
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); // "John Doe"

Arrays

An array in JavaScript is used to store multiple values in a single variable.

Creating an Array

let fruits = ["Apple", "Banana", "Orange"];

Accessing Elements

console.log(fruits[0]); // Apple
console.log(fruits[2]); // Orange

Length

console.log(fruits.length); // 3

Modifying Elements

fruits[1] = "Mango";
console.log(fruits); // ["Apple", "Mango", "Orange"]

Common Array Methods

  • push() – add at end

    fruits.push("Grapes");
  • pop() – remove from end

    fruits.pop();
  • unshift() – add at start

    fruits.unshift("Pineapple");
  • shift() – remove from start

    fruits.shift();
Back to Blog

Related posts

Read more »

Functions And Arrow Functions

What are functions? If we want to put it in simple words, they are one of the main building blocks of JavaScript. They are used to organize your code into smal...

Core Premise of Function in JavaScript

Is this a function in JavaScript? javascript function fiveSquared { return 5 5; } Technically, yes. However, fiveSquared lacks the reusability that a real‑world...