DEV Community

Saravanan Cn
Saravanan Cn

Posted on

Day 18: Learning JavaScript addEventListener(), localStorage & Nested Function Calls 🚀

1. addEventListener()

I learned how to use addEventListener() to respond to events such as button clicks.

let button = document.getElementById("btn");

button.addEventListener("click", () => {
console.log("Button clicked!");
});

2. localStorage

I learned about localStorage, which allows us to store data in the browser so that it can remain available after refreshing or reopening the page.

localStorage.setItem("username", "Saravanan");

let name = localStorage.getItem("username");

console.log(name);

Here****

  • setItem() stores data.
  • getItem() retrieves stored data.

3. Calling Another Function Inside an Arrow Function

I learned that an arrow function can call another function. This allows us to reuse code and organize our programs.

function greet() {
console.log("Hello!");
}

let showMessage = () => {
greet();
};

showMessage();

Here:

greet() is a function declaration.

showMessage is an arrow function.

Inside the arrow function, greet() is called.

Calling showMessage() executes the arrow function, which then calls greet().

Note: Calling another function inside a function does not automatically make it a nested function. A nested function is specifically a function defined inside another function.

Top comments (0)