0% found this document useful (0 votes)
2 views

js map

Uploaded by

Najmudeen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

js map

Uploaded by

Najmudeen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Nullish Opr

let name=prompt("Enter the Name:")


if(name)
console.log(my??"you didn't enter your name")
else
console.log("you didn't enter your name else")

function calculateTax(cost,tax){
taxamt = cost*(tax/100)
console.log('Total cost is : ${cost}
GST of tax($(tax}%) is: $(taxamt}
Total amount is : ${ cost+tax})

calculateTax(45,12);

function calculateTax(cost,tax){
tax = tax ?? 18;

taxamt = cost*(tax/100)
console.log('Total cost is : ${cost}
GST of tax($(tax}%) is: $(taxamt}
Total amount is : ${ cost+tax})

calculateTax(45);

function calculateTax(cost,tax = 18){

taxamt = cost*(tax/100)
console.log('Total cost is : ${cost}
GST of tax($(tax}%) is: $(taxamt}
Total amount is : ${ cost+tax})

calculateTax(45);

Reference types:
Reference types are data types that store a reference to a value, rather than the actual value itself.
When a variable is assigned a reference type, it stores a reference to the value in memory, rather
than the value itself. This means that changes made to the variable affect the original value.

The most common examples of reference types in JavaScript are objects and arrays.

Object: This reference type stores a collection of key-value pairs and is defined using curly
braces { }.
Array: This reference type stores a collection of values and is defined using square brackets [ ].
When a reference type is passed as an argument to a function, a reference to the original value is
passed to the function, rather than a copy of the value. This means that changes made to the
value within the function affect the original value.

Pass-by-Value
When you pass a variable of a primitive data type as an argument to a function, a copy of the
variable's value is created and passed to the function. Any changes made to the copy inside the
function do not affect the original variable outside the function.

Example

function double(number) {
number = number * 2;
return number;
}

let num = 5;
let doubledNum = double(num);

console.log(num); // Output: 5
console.log(doubledNum); // Output: 10

Pass-by-Reference
In JavaScript, objects and arrays are passed by reference. This means that when you pass an
object or an array to a function, any changes made to the object or array inside the function also
affect the original object or array outside the function.

Passing Objects

In JavaScript, objects are passed into functions by reference. This means that when an object is
passed into a function, any changes made to the object inside the function will also affect the
original object outside the function. This is because the object reference is being passed, not the
object itself.

let person = { name: 'John', age: 30 };

function updatePerson(obj) {
obj.age = 31;
}

updatePerson(person);
console.log(person.age); // Output: 31

Set & Map


Set is an Object

A JavaScript Set object is a collection of unique values. Each value can only occur once in a Set.
A Set can hold any value of any data type
Sets can only hold unique values, while Arrays can hold duplicate values.
Sets are not ordered, while Arrays are ordered.
Sets are faster to add and remove items from than Arrays.
Syntax
let set1 = new Set([iterable]);

Example
Let arr = [1,1,2,3,4,5,5,5,10,8]
Let myset1 = new Set(arr)
console.log(arr)
console.log(myset1)

let myset2 = new set()


myset2.add(4)
myset2.add(5)
myset2.add(‘pqr’)
myset2.add({‘a’:1,’b’:2})
myset2.add(4)

console.log(myset2)
let ob = {‘a’:1,’b’:2}
myset.add(obj)
console.log(myset2)
console.log(myset2.sieze)
console.log(myset2.has(6))
console.log(myset2.delete(4))

JavaScript Set Methods


In the following table, we have listed all the properties of Set class −
Sr.No. Name & Description
1 add() This method insert elements into the set.
2 clear() This method removes removes all elements of the set.
3 delete() This method deletes a single element of the set.
4 difference() This method returns elements in first set but not in the second set.
5 entries() To get an iterator containing all set entries.
6 forEach() This method executes a provided function once for each value in this set.
7 has() This method indicates whether an element with the specified value exists or not.
8 intersection() This method returns the common elements in both sets.
9 keys() This method is an alias for values() method.
10 values() This method returns a new Set iterator object that containing values for each
element in a Set object.

Map
Map objects are collections of Key-value pairs
A key in the map may only occur once
Key or value can be object

const numbers = [1, 2, 3, 4, 5]


console.log(numbers)
let final = numbers.map(function (val){
return val * 2
});
console.log(final)

Sr.No. Name & Description


1 clear() This method removes all elements from a Map object.
2 delete() This method removes a specified element from a Map object by key.
3 entries() This method returns a new map iterator object that contains the [key, value] pairs.
4 forEach() This method executes a callback function once per each key/value pair in a Map
object.
5 get() This method is used to return a specified element from a Map object.
6 has() This method indicates whether an element with the specified key exists or not.
7 keys() This method returns a new Iterator object that contains the keys for each element in
the Map object.
8 set() This method insert the key-value pair to a Map object.
9 values() This method returns a new Iterator object that containing values of the Map
object.
JavaScript Map Constructor()
Following is the constructor of Map in JavaScript −
Sr.No. Name & Description
1 Map() To create a Map object.

Example: Creating new Map Object


In the example below, we have passed the 2D array containing the key-value pairs as an
argument of the Map() constructor.
After that, we traverse the map to get each value of the map and show in the output.
*****
A Web API is an application programming interface (API) for web. The concept of the Web API
is not limited to JavaScript only. It can be used with any programming language. Let’s learn
what web API is first.
What is Web API?
The API is an acronym for the Application Programming Interface. It is a standard protocol or
set of rules to communicate between two software components or systems.
A web API is an application programming interface for web.
The API provides an easy syntax to use the complex code. For example, you can use the
GeoLocation API to get the coordinates of the users with two lines of code. You don’t need to
worry about how it works in the backend.
Another real-time example you can take is of a power system at your home. When you plug the
cable into the socket, you get electricity. You don’t need to worry about how electricity comes
into the socket.
There are different types of web APIs, some are as follow −
Browser API (Client-Side JavaScript API)
Server API
Third Party APIs
Browser API (Client-side JavaScript API)
The browser APIs are set of Web APIs that are provided by the browsers.
The browser API is developed on top of the core JavaScript, which you can use to manipulate the
web page's functionality.
There are multiple browser APIs available which can be used to interact with the web page.
list of common browser APIs −
Storage API − It allows you to store the data in the browser's local storage.
DOM API − It allows you to access DOM elements and manipulate them.
History API − It allows you to get the browser’s history.
Fetch API − It allows you to fetch data from web servers.
Forms API − It allows you to validate the form data.
Server API
A server API provides different functionalities to the web server. Server APIs allow developers
to interact with server and access data and resources.
For example, REST API is a server API that allows us to create and consume the resources on
the server. A JSON API is popular API for accessing data in JSON format. The XML API is a
popular API for accessing data in XML format.

Third-party APIs
The third-party API allows you to get the data from their web servers. For example, YouTube
API allows you to get the data from YouTube’s web server.
list of common third-party APIs.
YouTube API − It allows you to get YouTube videos and display them on the website.
Facebook API − It allows you to get Facebook posts and display them on the website.
Telegram API − It allows you to fetch and send messages to the telegram.
Twitter API − It allows you to get tweets from Twitter.
Pinterest API − It allows you to get Pinterest posts.

Fetch API: An Example of Web API


Here is an example of how to use the fetch API. In the below example, we Fetch API to access
data from a given URL ('https://jsonplaceholder.typicode.com/todos/5). The fetch() method
returns a promise that we handle using the “then” block. First, we convert the data into the JSON
format. After that, we convert the data into the string and print it on the web page.
JavaScript Web API List
Here, we have listed the most common web APIs.
API Description
Console API It is used to interact with the browser’s console.
Fetch API It is used to fetch data from web servers.
FullScreen API It contains various methods to handle HTML elements in full-screen mode.
GeoLocation API It contains the methods to get the user’s current location.
History API It is used to navigate in the browser based on the browser’s history.
MediaQueryList API It contains methods to query media.
Storage API It is used to access the local and session storage.
Forms API It is used to validate the form data.

You might also like