0% found this document useful (0 votes)
6 views22 pages

Lecture JS - Javascript APIs - Part 0

The document provides an overview of global objects in JavaScript, emphasizing their importance in data manipulation and asynchronous behavior. It details various global objects such as Object, Array, Date, Math, JSON, Promise, Set, and Map, along with their common methods and examples. Additionally, it discusses global functions like eval(), isNaN(), and URI encoding/decoding methods, highlighting the significance of the window object in the browser environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

Lecture JS - Javascript APIs - Part 0

The document provides an overview of global objects in JavaScript, emphasizing their importance in data manipulation and asynchronous behavior. It details various global objects such as Object, Array, Date, Math, JSON, Promise, Set, and Map, along with their common methods and examples. Additionally, it discusses global functions like eval(), isNaN(), and URI encoding/decoding methods, highlighting the significance of the window object in the browser environment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Global objects or Fundamental API

Introduction to Global Objects in


JavaScript
JavaScript has primitive types(int float string)
Object types include object, arrays, functions
Global objects are built-in objects always available in any scope.
 Essential for manipulating data, interacting with the environment, and managing
asynchronous behavior.
 Available by default in the browser environment (through window).
Global properties
Global objects are initialized with global constants like undefined, Infinity, NaN
Global functions isNan, parseInt, eval()
The Global Object
(window)
•Definition:
◦ In the browser, the window object represents the global object
◦ All global variables, functions , and objects are properties of window.
•Example:
console.log(window); // The global window object
Object - Base of All JavaScript
Objects
•What is Object?
•The root object for all other objects.
•Common Methods:
•Object.keys(): Returns an array of an object's keys.
•Object.values(): Returns an array of an object's values.
•Example:
let obj = { name: "Alice" };
console.log(Object.keys(obj)); // ["name"]
Array - Working with
Arrays
•What is Array?
•A global object used to store and manage ordered collections of values.
•Common Methods:
•Array.push(), Array.map(), Array.filter().

•Example:
let arr = [1, 2, 3]; let doubled = arr.map(x => x * 2);
console.log(doubled); // [2, 4, 6]
Date - Working with Dates
and Times

•What is Date?
•Provides methods for handling dates and times.
•Common Methods:
•Date.now(), Date.getFullYear(), Date.getTime().

•Example:
let today = new Date(); console.log(today.getFullYear()); // Current year
Math - Mathematical
Operations
•What is Math?
•Provides mathematical constants and functions.
•Common Methods:
•Math.random(), Math.max(), Math.PI.

•Example:
console.log(Math.random()); // Random number between 0 and 1
JSON - Working with
JSON
•What is JSON?
•Provides methods for parsing and stringifying JSON (JavaScript Object Notation).
•Common Methods:
•JSON.parse(), JSON.stringify().

•Example:
let obj = { name: "Alice" }; let jsonString = JSON.stringify(obj);
console.log(jsonString); // '{"name":"Alice"}'
Promise - Asynchronous
Operations
•What is a Promise?
•Represents the eventual completion (or failure) of an asynchronous operation.
•Methods:
•Promise.resolve(), Promise.reject(), Promise.all().

•Example:
let myPromise = new Promise((resolve, reject) => { let success = true; success ? resolve("Success!") : reject("Error!"); });
myPromise.then(result => console.log(result)).catch(error => console.log(error));
Set - Unique
Collections
•What is Set?
•A collection of unique values.
•Common Methods:
•Set.add(), Set.has(), Set.delete().

•Example:
let mySet = new Set();
mySet.add(1).add(2);
console.log(mySet.has(1)); // true
Map - Key-Value Pair
Collections
•What is Map?
•A collection of key-value pairs.
•Common Methods:
•Map.set(), Map.get(), Map.delete().

•Example:
let myMap = new Map();
myMap.set('name', 'Alice’);
console.log(myMap.get('name')); // Alice
RegExp - Regular
Expressions
•What is RegExp?
•Provides functionality to work with regular expressions.
•Common Methods:
•RegExp.test(), RegExp.exec().

•Example:
let regex = /hello/;
console.log(regex.test("hello world")); // true
Error - Error
Handling
•What is Error?
•Used for creating error objects and handling exceptions.

•Example:
let err = new Error("Something went wrong!");
console.log(err.message); // Something went wrong!
localStorage -
Storing Data
•What is localStorage?
•A web storage object used to store data locally in the user's browser.

•Example:
localStorage.setItem("name", "Alice");
console.log(localStorage.getItem("name")); // Alice
Global functions
eval() - Evaluating
Code
•What is eval()?
•Executes JavaScript code represented as a string.
•Note: Use with caution due to security risks.

•Example:
let x = 2; eval('x = x + 3’);
console.log(x); // 5
isNaN() - Checking
for NaN
•What is isNaN()?
•Checks if a value is NaN (Not-a-Number).

•Example:
console.log(isNaN('hello')); // true
console.log(isNaN(42)); // false
decodeURI() / encodeURI() - URI
Encoding and Decoding

•What are decodeURI() and encodeURI()?


•Functions for encoding and decoding URI components.
•Example:
let uri = "https://example.com/?name=John Doe"; let encoded = encodeURI(uri);
console.log(encoded); // Encodes the URI
decodeURIComponent() /
encodeURIComponent()
•What are decodeURIComponent() and encodeURIComponent()?
•These functions are used for encoding and decoding individual URI components
(such as query parameters).

•Example:
let param = "name=John Doe"; let encodedParam = encodeURIComponent(param);
console.log(encodedParam); // name%3DJohn%20Doe
window Methods for Interacting
with the Browser
•Common Methods:
•window.alert(): Displays an alert box.
•window.setTimeout(): Calls a function after a specified delay.
•window.setInterval(): Repeatedly calls a function with a fixed time
delay.

•Example:
window.alert("This is an alert!");
window.setTimeout(() => { console.log("Hello!"); }, 1000);
Conclusion - Key Takeaways
Global Objects are essential for JavaScript development in the browser.
window is the global object in browsers, which provides access to many built-in functions and
objects.
Commonly Used Objects: Object, Array, Date, Math, JSON, Promise, Set, Map, and more.
Experiment with these objects to unlock JavaScript's full potential in your projects.

You might also like