DAY-4
JAVASCRIPT
JavaScript Fundamentals: Day 4
     30 Days Of JavaScript
Greetings and welcome back to our thrilling ” 30 Days of Javascript” Today, on Day 4, we’re digging deep into the principles of JavaScript. You’re getting better at JavaScript every day, and today’s excursion looks to be nothing short of enchanting.Â
We will summon the beauty of functions, harness the might of operators and expressions, discover the mysteries of conditional statements and loops, and explore this beautiful world. As we explore the fascinating world of JavaScript, get ready to wave your coding magic wand.Â
Section 1: Operators and Expressions – The Language of Enchantment
JavaScript’s intriguing language is built around operators. They give you the ability to do amazing operations on variables and values. You open a door to a universe of possibilities with each operator.
Example Code:
let num1 = 5;
let num2 = 3;
let sum = num1 + num2; // Active Voice: We add num1 and num2 to get the sum.
console.log(sum); // Output: 8
Here, we used the addition operator (+) to add num1 and num2, storing the result in the variable sum. The active voice empowers our code, clarifying each step of the enchantment.
 Youtube Video :
Section 2: Conditional StatementsÂ
 In the mystical land of JavaScript, conditional statements guide your code’s destiny. They allow your spells to make choices based on conditions, taking your applications down different paths.
Example Code:
let hour = 15;
let greeting;
if (hour < 12) {
 greeting = "Good morning!"; // Active Voice: We set the greeting to "Good morning!"
} else {
 greeting = "Good afternoon!"; // Active Voice: We set the greeting to "Good afternoon!"
}
console.log(greeting); // Output: "Good afternoon!"
Here, we used an if statement to set the greeting based on the hour. If the hour is less than 12, the greeting is set to “Good morning,” otherwise, it’s set to “Good afternoon.“
Section 3: Loops – Unraveling Infinite Possibilities
Loops hold the key to unlocking infinite possibilities in JavaScript. They allow you to perform repetitive tasks and explore the vastness of your code.
Example Code:
for (let i = 1; i <= 5; i++) { Â console.log("Count: " + i); // Active Voice: We print the current count. }}}Â
In this example, we used a for loop to print the count from 1 to 5. The active voice helps us understand each iteration’s purpose, creating clarity in our code.
Section 4: Functions – The Elegance of Reusable Magic
Functions are the embodiment of elegance in JavaScript. They encapsulate blocks of code, making your spells reusable and your codebase organized.
Example Code:
function greet(name) { return "Hello, " + name + "!"; // Active Voice: We return a personalized greeting. } let greeting = greet("John"); console.log(greeting); // Output: "Hello, John!"
Here, we defined a function called greet that takes a name parameter and returns a personalized greeting. The active voice clarifies our function’s purpose, making it easier to comprehend.
Â
Section 5: Variable Scope – The Boundaries of Your Magic Kingdom
Variable scope is the realm that governs the visibility and accessibility of variables in JavaScript. Understanding it is essential for controlling your magical creations.
Example Code:
javascriptCopy code let globalSpell = "PWSC"; // Active Voice: We declare a global spell. function castSpell() { Â let localSpell = "Updategadh"; // Active Voice: We declare a local spell inside the function. console.log(globalSpell + " " + localSpell); // Output: "Abracadabra Alakazam"} castSpell(); console.log(globalSpell); // Output: "Abracadabra" console.log(localSpell); // Error: localSpell is not defined
In this example, the globalSpell is accessible throughout the entire code, while localSpell is only visible within the castSpell function. The active voice emphasizes each spell’s scope, bringing clarity to our incantations.
Â
Section 6: JavaScript in the Real World – Enchanting Applications
The true magic of JavaScript shines when applied to the real world. Websites come alive with interactivity, captivating users with every click and scroll.
Example Code:
javascriptCopy code const button = document.getElementById("myButton"); button.addEventListener("click", () => { Â alert("You clicked the button!"); // Active Voice: We show an alert when the button is clicked.});
In this example, JavaScript’s enchantment adds an event listener to a button element, displaying an alert when clicked. The active voice reinforces each step of this real-world magic.
Youtube Video :https://www.youtube.com/watch?v=_JPsTXAAUUo&list=PL92c0di50EKCw-5ugqKhp2rOkGiBslPHI&pp=iAQB
 30 Days Of JavaScript
Post Views: 333
Post Comment