0% found this document useful (0 votes)
4 views4 pages

BSC 4 JS Example

The document explains arrow functions in JavaScript, highlighting their concise syntax and lexical binding of 'this', making them ideal for callbacks. It also covers Promises, which manage asynchronous operations, providing examples of their usage, including a countdown timer. Overall, it illustrates how both features enhance JavaScript programming efficiency and readability.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

BSC 4 JS Example

The document explains arrow functions in JavaScript, highlighting their concise syntax and lexical binding of 'this', making them ideal for callbacks. It also covers Promises, which manage asynchronous operations, providing examples of their usage, including a countdown timer. Overall, it illustrates how both features enhance JavaScript programming efficiency and readability.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARROW FUNCTIONS:

Arrow functions, introduced in ES6, provide a concise and readable way to write function
expressions in JavaScript. They use the => syntax, which not only reduces boilerplate but
also binds this lexically, making them particularly useful in certain scenarios like handling
callbacks or working within objects.

NORMAL FUNCTION:

// Normal function to add two numbers


function add(a, b) {
return a + b;
}

// Calling the function


[Link](add(5, 3)); // Output: 8

ARROW FUNCTION:

// Arrow function to add two numbers


const add = (a, b) => {
return a + b;
};

// Calling the function


[Link](add(5, 3)); // Output: 8

SHORT FORM OF ARROW:

const add = (a, b) => a + b;

[Link](add(5, 3)); // Output: 8


PROMISE in JAVASCRIPT:

A Promise in JavaScript is used to handle asynchronous operations (like API calls, file loading,
timers).

<script>
// Creating a Promise
let myPromise = new Promise(function(resolve, reject)
{
let success = true;

if (success)
{
resolve("Task completed successfully");
}
else
{
reject("Task failed");
}
});

// Using the Promise


myPromise
.then(function(result)
{
[Link](result);
})

.catch(function(error)
{
[Link](error);
});
</script>

Output (if success = true):

Task completed successfully


<script>

// Promise with timer example


let timerPromise = new Promise(function(resolve, reject)

setTimeout(function()

resolve("Timer completed after 3 seconds");

}, 3000);

});

// Using the Promise

timerPromise

.then(function(message)

[Link](message);

})

.catch(function(error)

[Link](error);

});

</script>

Output after 3 seconds:

Timer completed after 3 seconds


<script>
// Countdown timer using Promise
function countdown(seconds)
{
return new Promise(function(resolve)
{
let timeLeft = seconds;

let timer = setInterval(function()


{
[Link](timeLeft);

timeLeft--;

if (timeLeft < 0)
{
clearInterval(timer);
resolve(" Countdown Finished!");
}
}, 1000);
});
}

// Calling the function


countdown(5).then(function(message)
{
[Link](message);
});

</script>

Sample Output:
5
4
3
2
1
0
Countdown Finished!

You might also like