दोस्तों आज हम बात करेंगे JavaScript के 10 ऐसा functions के बारे में जिनको जानना एक Web Developer या फिर वर्डप्रेस डेवलपर के लिए बहुत ही जरूरी है ।
हम जितने भी फंक्शंस के बारे में अभी डिस्कस करेंगे उनको हम Example के साथ देखेंगे और आप यहां से Source
Code को कॉपी करके अपने साइट्स में यूज कर सकते हैं।
तो चलिए सुरु करते है और जन लेते है उन functions के बारे में |
1) - Replace Function -
Replace() के द्वारा हम किसी भी सिलेक्टेड स्ट्रिंग को रिप्लेस कर सकते हैं |
यह फ़ंक्शन मूल स्ट्रिंग को नहीं बदलता है।
Syntax -
string.replace(searchValue, newValue)
Example -
let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "sknetking");
This text will change after on click.
2) - href :-
Get the URL of the current page;
इस function की help से हम करंट पेज की full URL पा सकते है |
Syntex -:
let url = location.href;
Example -
function myFunction() {
var url = location.href;
alert(url);
}
As a language, JavaScript provides a wide range of built-in functions, but there are also many custom functions that developers can create to add functionality to their applications. Here are some essential JavaScript functions that every developer should know:
querySelector: This function returns the first element within the document that matches the specified CSS selector.
Syntax -
const element = document.querySelector('.example-class');
addEventListener: This function attaches an event listener to a specified element.
Syntax -
element.addEventListener('click', function() {
console.log('Element clicked');
});
setTimeout: This function executes a function after a specified number of milliseconds.
Syntax -
setTimeout(function() {
console.log('Function executed after 1000 milliseconds');
}, 1000);
setInterval: This function executes a function repeatedly after a specified number of milliseconds.
Syntax -
setInterval(function() {
console.log('Function executed every 1000 milliseconds');
}, 1000);
JSON.stringify: This function converts a JavaScript object to a JSON string.
Syntax -
const obj = { name: 'John', age: 30 };
const jsonString = JSON.stringify(obj);
JSON.parse: This function converts a JSON string to a JavaScript object.
Syntax -
const jsonString = '{"name": "John", "age": 30}';
const obj = JSON.parse(jsonString);
fetch: This function is used to make HTTP requests to a server and return a Promise.
Syntax -
fetch('https://example.com/data')
.then(response => response.json())
.then(data => console.log(data));
Array.map:
This function creates a new array with the results of calling a provided function on every element in the original array.
Syntax -
const numbers = [1, 2, 3];
const doubledNumbers = numbers.map(function(number) {
return number * 2;
});
console.log(doubledNumbers); // Output: [2, 4, 6]
Array.filter:
This function creates a new array with all elements that pass the test implemented by the provided function.
Syntax -
const numbers = [1, 2, 3, 4, 5];
const filteredNumbers = numbers.filter(function(number) {
return number > 2;
});
console.log(filteredNumbers); // Output: [3, 4, 5]
Array.reduce:
This function applies a function against an accumulator and each element in the array to reduce it to a single value.
Syntax -
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
});
console.log(sum); // Output: 15
These are just a few of the many essential JavaScript functions that every developer should know. As you become more proficient in JavaScript, you will discover many more functions and features that can be used to create robust and efficient applications.
0 Comments