Don't understand how parameters work in JavaScript?

Good morning,
How exactly do parameters work?
Do parameters differ whether they are in a loop, an array or a function?

And how exactly, especially in real-life examples, is the parameter assigned a value?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
NoArtFX
1 year ago

Do parameters distinguish whether they are in a loop, an array or a function?

There are no parameters outside of functions. What you mean Variable.

Maybe explain your whole terms individually.

Grind:
Here there are basically 2 types of loops:

The and Grind and the for Bow.

:

while(BEDINGUNG) { // führe aus }

In this case, a condition is passed which is evaluated in a chest/fold.

For example age < 18.

This can be either true or wrong, based on which value the Variable age has.

As long as the condition is true, the code is executed within the loop. You can best change the variable(s) of the condition so that this “close” returns to the heel to prevent an endless loop.

For:

for (let i = 0; i < 3; i++) { // führe aus }

One for loop is nothing but one and loop with integrated variable, only here is Scope different. You can't go outside the loop on yours Variable I have access to the and Bow.

Array

A Array is a list of values of any data type (in Javascript!). For example, you can save people, recipes, invoices, numbers, letters, words, etc.

Important: Arrays become 0 indexed. That means the Index is the first value. bsp. let arr = [1,2,3,4]; console.log(arr[0]) // 1;

Functions

A function does not accept any, one or x as many parameters and gives an output.

Bsp:

function add(a, b) {
  return a + b;
}
add(1,2) // gibt 3 zurück

a and b are yours Arguments, within the function they become Parameters called.

By calling add(1,2), you transfer values to your function and then receive as Return value 3

Chris102004
1 year ago
Reply to  NoArtFX

That’s right!

NoArtFX
1 year ago
Reply to  MarlonStrich

No a and b in the brackets are parameters. The mode of operation is similar, but not the same. Variables are used to store values and use them later. Parameters are used only within the respective function. When the function is called, they will be handed over and forgotten again.

aloscha211
1 year ago

In JavaScript, parameters are used to transfer data to a function. These parameters serve as space holders for values that are provided when the function is called up. The function of parameters does not differ depending on whether they are used in a loop, an array or a function. However, it is important to understand how they can be used in different contexts.

Here are some examples to help you better understand the functioning of parameters in JavaScript:

  1. Parameters in one function:
javascript
function add(a, b) { return a + b; } console.log(add(2, 3)); // Ausgabe: 5
  1. In this example,
  2. a
  3. and
  4. b
  5. Parameters of the function
  6. add
  7. . When calling the function
  8. add(2, 3)
  9. the values
  10. 2
  11. and
  12. 3
  13. to the parameters
  14. a
  15. and
  16. b
  17. over.
  18. Parameters in a loop:
javascript


function printArray(array) { for (let i = 0; i < array.length; i++) { console.log(array[i]); } } printArray([1, 2, 3, 4]);
  1. Here's
  2. array
  3. the parameter of the function
  4. printArray
  5. . When the function is called, an array
  6. [1, 2, 3, 4]
  7. and the parameters
  8. array
  9. contains this array.
  10. Parameters in an array:
javascript


function greet(names) { names.forEach(name => { console.log(`Hello, ${name}!`); }); } greet(["Alice", "Bob", "Charlie"]);
  1. In this example,
  2. names
  3. the parameter of the function
  4. greet
  5. . When calling the function, an array with name is passed, and the parameter
  6. names
  7. contains this array.

In each of these examples, the values are assigned to the parameters when calling the corresponding function. When the function is called, the transferred values are assigned to the parameters according to the order or assignment.

 Mirko Marek
1 year ago

Hi,

Parameters can help to override values or objects in the function call. Here are some classic examples of me in JavaScript:

let ergebnis = rechne(5,5);
function rechne(a, b) {
return a + b
}

const beispiel = ['Hallo ','Welt ','wie ','geht ', 'es ', 'dir?'];
zeigeString(beispiel);

function zeigeString(myArray) {
let meinString;
for(item of myArray) {
meinstring += item;
}
alert(meinString);
}

let nachricht = "Hallo Welt";
zeigeNachricht(naricht);
function zeigeNachricht(message) {
alert(message);
}

Various information can be transmitted to functions/methods.