Javascript Articles

Page 9 of 534

Usage of rest parameter and spread operator in JavaScript?

Anjana
Anjana
Updated on 11-Mar-2026 381 Views

Rest ParameterWith rest parameter, you can represent a number of arguments as an array. ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and precedes a parameter.Let’s see the following code snippet to define rest parameter −                    function addition(…numbers) {             var res = 0;             numbers.forEach(function (number) {                res += number;             });     ...

Read More

What are the latest operators added to JavaScript?

Ayyan
Ayyan
Updated on 11-Mar-2026 148 Views

The latest operators added to JavaScript are spread operator and rest.Rest operatorWith rest parameter, you can represent number of arguments as an array. ES6 brought rest parameter to ease the work of developers. For arguments objects, rest parameters are indicated by three dots … and preceds a parameter.ExampleLet’s see the following code snippet to define rest parameter                    function addition(…numbers) {             var res = 0;             numbers.forEach(function (number) {                res += number;   ...

Read More

How to define getter and setter functions in JavaScript?

Ayyan
Ayyan
Updated on 11-Mar-2026 355 Views

GetterWhen a property is accessed, the value gets through calling a function implicitly. The get keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.SetterWhen a property is set, it implicitly call a function and the value is passed as an argument. With that the return value is set to the property itself. The set keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.ExampleHere’s an example showing how to implement both getter and setter                    var ...

Read More

How to delete a setter using the delete operator in JavaScript?

Manikanth Mani
Manikanth Mani
Updated on 11-Mar-2026 289 Views

To delete a setter using the delete operator, use the delete keyword. Here’s how you can delete −delete obj.nameExampleYou can try to run the following code to learn how to delete a setter                    var department = {             deptName: "Marketing",             deptZone: "North",             deptID: 101,             get details() {                return "Department Details" + "Name: " + this.deptName + " Zone: " + ...

Read More

What is the difference between getter and setter in JavaScript?

Akshaya Akki
Akshaya Akki
Updated on 11-Mar-2026 1K+ Views

GetterWhen a property is accessed, the value gets through calling a function implicitly. The get keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.SetterWhen a property is set, it implicitly calls a function and the value is passed as an argument. With that, the return value is set to the property itself. The set keyword is used in JavaScript. An identifier, either a number or a string is allowed for set.ExampleHere’s an example showing how to implement both getter and setter                    var ...

Read More

What is the JavaScript version of sleep()?

Alankritha Ammu
Alankritha Ammu
Updated on 11-Mar-2026 462 Views

The JavaScript version of sleep() is “await”. The await feature pauses the current aync function.ExampleYou can try to run the following code to implement sleep in JavaScript                    function setSleep(ms) {             return new Promise(resolve => setTimeout(resolve, ms));          }          async function Display() {             document.write('Wait for 5 seconds!');             await setSleep(5000);             document.write('After 5 seconds!');          }          Display();          

Read More

How to Scroll to the top of the page using JavaScript/ jQuery?

Anjana
Anjana
Updated on 11-Mar-2026 310 Views

Example                    $("a").click(function() {             $("html, body").animate({ scrollTop: 0 }, "slow");             return false;          });             TOP OF PAGE             This is demo text.       This is demo text.       This is demo text.       This is demo text.       This is demo text.       This is demo text.       This is demo ...

Read More

How to use variable number of arguments to function in JavaScript?

Krantik Chavan
Krantik Chavan
Updated on 11-Mar-2026 697 Views

To use a variable number of arguments to function, use the arguments object.ExampleYou can try to run the following code to implement arguments to a function in JavaScript                    function functionArgument(val1, val2, val3)  {             var res = "";             res += "Expected Arguments: " + functionArgument.length;             res += "";             res += "Current Arguments : " + arguments.length;             res += "";             res += "Each argument = "             for (p = 0; p < arguments.length; p++) {                res += "";                res += functionArgument.arguments[p];                res += " ";             }             document.write(res);          }          functionArgument(20, 50, 80, "Demo Text!","Hello World", new Date())          

Read More

What is &quot;function*&quot; in JavaScript?

Smita Kapse
Smita Kapse
Updated on 11-Mar-2026 338 Views

The function* declaration is used to define a generator function. It returns a Generator object. Generator Functions allows execution of code in between when a function is exited and resumed later. So, generators can be used to manage flow control in a code.SyntaxHere’s the syntax −function *myFunction() {} // or function* myFunction() {} // or function*myFunction() {}Let’s see how to use generator functionExample                    function* display() {             var num = 1;             while (num < 5)             yield num++;          }          var myGenerator = display();          document.write(myGenerator.next().value);          document.write(""+myGenerator.next().value);          document.write(""+myGenerator.next().value);          document.write(""+myGenerator.next().value);          document.write(""+myGenerator.next().value);          

Read More

What is the use of JavaScript eval function?

Vrundesha Joshi
Vrundesha Joshi
Updated on 11-Mar-2026 400 Views

The JavaScript eval() is used to execute an argument. The code gets execute slower when the eval() method is used. It also has security implementations since it has a different scope of execution. In addition, use it to evaluate a string as a JavaScript expression.The eval() method is not suggested to use in JavaScript since it executes slower and improper usage opens your website to injection attacks.ExampleHere’s how you can implement eval() function                    var a = 30;          var b = 12;          var res1 = eval("a * b") + "";          var res2 = eval("5 + 10") + "";          document.write(res1);          document.write(res2);          

Read More
Showing 81–90 of 5,339 articles
« Prev 1 7 8 9 10 11 534 Next »
Advertisements