0

When to use and not use single or double quotes within functions?

for example:

function convertToInteger(str) {
  return parseInt(str); // why do we not use double-quotes here? is this as simple as we never use quotes around arguments when calling a function?
}

convertToInteger("56");
Dharman
  • 30,962
  • 25
  • 85
  • 135
Zak
  • 23
  • 2
  • 3
    `str` is a [variable](//developer.mozilla.org/en/docs/Glossary/Variable), `"str"` is a [string literal](//developer.mozilla.org/en/docs/Glossary/String). By the way, please use `parseInt` [_with_ the second parameter, `10`](/q/16880327/4642212). Consider using [`Number`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number#Function_syntax) or [`parseFloat`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) instead. – Sebastian Simon Mar 22 '22 at 16:40
  • 1
    read this perhaps to learn about functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions – buga Mar 22 '22 at 17:00

2 Answers2

1

the variables inside the function are called arguments. They serve to store the item you passed. As you passed it, it will use whatever value you enter, but if you put a value in quotes, you would be setting a fixed value.

  • 1
    "*the variables inside the function are called arguments.*" they are called *parameters* and only when part of the function definition. There can be non-parameter variables in the body. Argument is what is *passed in* to a function. So `function fn(a, b) {}` defines parameters, `fn(1, 2)` passes arguments. – VLAZ Mar 22 '22 at 16:58
  • Sorry for the mistakes, but thanks for understanding. – Pedro Cardoso Mar 22 '22 at 17:56
0

Variable value should be in quotes , variable name should not be in quotes .

convertToInteger("56");

or

var datavalue="56";

convertToInteger(datavalue);

Dhirendra
  • 24
  • 1