
👉What Are 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
}
Example: Counting Vowels in a Name
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 Function with Return Value
JavaScript Functions: Functions can return values using the return statement.
Syntax:
javascript
function functionName(parameter) {
return value; // Returns a result
}
Example: Adding Two Numbers
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>
👉JavaScript Cookies: Set, Get & Delete
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.
Why Use Cookies?
- Maintain user sessions
- Store user preferences
- Keep users logged in
How to Set a Cookie in JavaScript
Use document.cookie to create a cookie.
Example: Set a Cookie with Expiration Date
javascript
document.cookie = “username=John; expires=Fri, 31 Dec 2025 12:00:00 UTC; path=/”;
👉How to Get Cookies in JavaScript
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”));
👉How to Delete a Cookie in JavaScript
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=/”;
👉Conclusion
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.