---Advertisement---

Complete SEO-Optimized Article: JavaScript Functions & Cookies with Examples Best 2025

By Manisha

Updated On:

---Advertisement---
JavaScript Functions

JavaScript Functions: A function in JavaScript is a reusable block of code that executes only when called. Functions help make code modular, reducing redundancy and improving maintainability.

How to Create a Function in JavaScript

To define a function, use the function keyword, followed by the function name, parentheses, and curly braces {}.

Syntax:

javascript

function functionName() {

    // Code to be executed

}

Example: Simple Function in JavaScript

html

<html>

<head>

   <title>JavaScript Functions</title>

   <script type=”text/javascript”>

      function greetUser() {

         document.write(“Hello, welcome to JavaScript functions!<br>”);

      }

      greetUser();  // Calling the function

   </script>

</head>

<body>

</body>

</html>

JavaScript Function with Arguments

JavaScript Functions: Functions can accept parameters (arguments) that allow passing data dynamically.

Syntax:

javascript

function functionName(parameter1, parameter2) {

    // Code using parameters

}

html

<html>

<head>

   <script type=”text/javascript”>

      function countVowels(name) {

         let count = 0;

         for (let i = 0; i < name.length; i++) {

             if (“aeiou”.includes(name[i].toLowerCase())) {

                 count++;

             }

         }

         document.write(“Hello ” + name + “! Your name has ” + count + ” vowels.”);

      }

      let myName = prompt(“Please enter your name”);

      countVowels(myName);

   </script>

</head>

<body>

</body>

</html>

JavaScript Functions: Functions can return values using the return statement.

Syntax:

javascript

function functionName(parameter) {

    return value; // Returns a result

}

html

<html>

<head>

   <script type=”text/javascript”>

      function sumNumbers(a, b) {

         return a + b;

      }

      let result = sumNumbers(10, 20);

      document.write(“Sum: ” + result);

   </script>

</head>

<body>

</body>

</html>


What Are Cookies in JavaScript?

A cookie is a small data file stored in the user’s browser to remember information such as login credentials, preferences, or session data.

  • Maintain user sessions
  • Store user preferences
  • Keep users logged in

How to Set a Cookie in JavaScript

Use document.cookie to create a cookie.

javascript

document.cookie = “username=John; expires=Fri, 31 Dec 2025 12:00:00 UTC; path=/”;


Retrieve cookies using document.cookie.

Example: Get a Cookie

javascript

function getCookie(name) {

    let cookies = document.cookie.split(“; “);

    for (let cookie of cookies) {

        let [key, value] = cookie.split(“=”);

        if (key === name) return value;

    }

    return null;

}

console.log(getCookie(“username”));


To delete a cookie, set an expiration date in the past.

Example: Delete a Cookie

javascript

document.cookie = “username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/”;


JavaScript functions and cookies are essential for web development. Functions improve code reusability, while cookies help in session management. Understanding how to create, call, set, get, and delete these elements will enhance your JavaScript skills.


This optimized content is SEO-friendly with relevant keywords, clear structure, and examples that improve user engagement and readability.

Java Script Condition
Download JS

Leave a Comment

Index