
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is it possible to change the value of the function parameter in JavaScript?
In this article we are going to find out whetehr it is possibleto change the value of the function parameter in JavaScript.
A function accepts a certain values as parameters an later while calling this function we need to pass values for these parameters as arguments. Typically a function definition contains the name of the function, parameters (optional) and the body of the function.
Syntax
Following is the syntax for function parameter −
function Name(paramet1, paramet2, paramet3,paramet4) { // Statements }
Yes, it is possible to change the value of the function parameter in JavaScript. increment and assign to some other variable and can return the incremented value. Let’s jump into the following example to understand more about the topic.
Example
In the following example we are running the script to change the value of the function parameter.
<!DOCTYPE html> <html> <body> <script> function increaseB(b) { var c = b + 1; return c; } var b = 3; document.write('Actual:', b); b = increaseB(b); document.write('<br>Modified:', b); </script> </body> </html>
When the program is executed, a browser window is displays, the actual and modified values on the web browser.
Example
Consider another example, where we are changing the value of the attributes of an object.
<!DOCTYPE html> <html> <body> <p id="tutorial"></p> <p id="tutorial1"></p> <script> function change(x) { x.x = 2; } let y = { x: 1 }; document.getElementById("tutorial").innerHTML= JSON.stringify(('Original:', y)) + ":Actual Value"; change(y); document.getElementById("tutorial1").innerHTML= JSON.stringify(('Modified:',y)) +":Modified Value"; </script> </body> </html>
The event gets triggered when the user runs the above script, resulting the actual value to change its value, and display as a modified one.
Example
Let’s look into the another example for changing the value of the function parameter.
<!DOCTYPE html> <html> <body> <script> var myText = "Bye"; changeText(myText); function changeText(variableToUse) { variableToUse = "Welcome"; document.write(myText); } </script> </body> </html>
When the script gets executed, the event gets triggered and changes the actual text “welcome” to “bye” and displays it.
Example
Let’s run the below code to see the output
<!DOCTYPE html> <html> <body> <script> function changeTheValueOfFunctionParameter(value) { var incrementValue = value + 1; return incrementValue; } var value = 190; document.write('The value is:', value, '<br>'); value = changeTheValueOfFunctionParameter(value); document.write('After changing the value:', value); </script> </body> </html>
The event gets triggered when the user executes the above code, and the changed value and actual value are displayed on the web-browser.
- Related Articles
- Is having the first JavaScript parameter with default value possible?
- Is it possible to change the HTML content in JavaScript?
- Is it possible to change values of the array when doing foreach() in javascript?
- Is it possible to instantiate Type-parameter in Java?
- How to set a default parameter value for a JavaScript function?
- Is it possible to give arguments in the main() function in C language?
- How to change the value of a global variable inside of a function using JavaScript?
- How to change the value of an attribute in javascript?
- How to test if a parameter is provided to a function in JavaScript?
- What MySQL ASCII() function returns if no parameter is provided to it?
- What MySQL CHAR_LENGTH() function returns if no parameter is provided to it?
- Is it possible to change directory by using File object in Java?
- How to define rest parameter for a function in JavaScript?
- Is it possible to have a function-based index in MySQL?
- Is it possible to manually set the attribute value of a Web Element using Selenium?
