JavaScript Day 9
JavaScript Day 9
await
"async and await make promises easier to
write"
Async
Syntax
How to
use?
Complete
example
The await keyword can only be used inside
an async function.
The await keyword makes the function pause the
Await execution and wait for a resolved promise before it
syntax continues:
Example
Example –
waiting for
a timeout
Module 8 - JavaScript
Error Handling
JavaScript error handling allows you
errors variable).
Example
Handling call back errors
Example 2
Example –
custom
function
compariso
n
Promise: “Here’s a ticket. Wait over there. I’ll call
cleared.
localStorage.
localStorage.setItem('key',
'value');
To retrieve the data you stored, use the
getItem() method.
Clearing
data
localStorage.clear()
;
You can check if a key exists in localStorage
by using getItem().
window.sessionStorage
;
Temporary Storage: Data is stored only for the
duration of the session.
Lifespan Persists even after browser is closed Clears when tab or browser is closed
Use Case Store data for long-term use Store data for temporary session
Example Data Login tokens, theme settings Form data, wizard progress
Accessible From Any tab of same origin Only within the tab that created it
Fetch data using API
•fetch() – to make the HTTP request
•JSON.parse() – to convert JSON text to a JavaScript object
Examples:
Stock prices
Train schedules
Live sports scores
https://jsonplaceholder.typicode.com/users fake
online REST API for testing and learning
purposes. It’s free and open to use.
syntax })
.catch(error => {
// Handle network errors (e.g., no internet,
timeout)
});
Scenario How to Handle
console.log(`Hello, ${name}`);
}
console.log(updatedStudent);
// Output: { name: 'Geetha', age: 21, city: 'Chennai' }
// Rest (gather)
function sum(...nums) {
}
Rest
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
optional };
console.log(user?.address?.city); // ✅ Chennai
console.log(user?.address?.zipcode); // ✅ undefined (no error)
console.log(user?.contact?.phone); // ✅ undefined (no error)
They allow you to split your code into
multiple files (called modules) and reuse
functionality between them, making your
project:
Import /
Easier to manage
export
Cleaner to maintain
math.js
Export export const PI = 3.14;
console.log(add(5, 10)); // 15
Greet.js
export default function greet(name) {
return `Hello, ${name}`;
}
Default
export import greet from './greet.js';
Full-fledged framework by
Lightweight, progressive
Vue.js framework. Easy to learn and
flexible.
Backend -
Use Case Recommended
Increment/decrement
✅ Counter App Event Listeners
with buttons
Enter amount & tip % to Input handling,
Project ideas ✅ Tip Calculator
get total Math
✅ Palindrome Check if word/number is String/Array
Checker a palindrome methods
Weather App
fetch, async/await City input + weather
(OpenWeather API)
🎨 Theme Switcher
CSS, localStorage Button toggle
(Dark/Light)