0

I am a beginner. I am learning about variables right now but I cannot understand what the second const in this code does. Can someone explain?

const name = "asus computer"
function toUpper(text) {
    const upperCased = text.toUpperCase();
    console.log(upperCased);
}

toUpper(name);
Nicholas Tower
  • 37,076
  • 4
  • 43
  • 60
emiratli
  • 11
  • 1
  • `the result of the text.toUpperCase() ?` or what do you mean? there will be the name with capital letters – Aalexander Jan 13 '21 at 16:18
  • `const upperCased = text.toUpperCase();` means "create a constant called upperCased and make its the value be the result of calling the text.toUpperCase function". Does that help? It's bit unclear what precisely you are struggling to understand. Can you clarify your issue? – ADyson Jan 13 '21 at 16:19
  • 1
    Also see [Are there constants in JavaScript?](https://stackoverflow.com/q/130396/4642212). See [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements), specifically [`const`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const). – Sebastian Simon Jan 13 '21 at 16:23
  • @ADyson thank you for your reply! We define a variable in the first line. Then define another one and make its value a function. Why don't we just create the function and call it? – emiratli Jan 13 '21 at 19:18
  • Do you mean why don't we just write `toUpper("asus computer");`? You could do that, if you wanted to (and if you didn't need to use `name` anywhere else in the code later on). – ADyson Jan 13 '21 at 19:21
  • @ADyson No, why don't we just write const name = "asus computer" function toUpper(text) { console.log(upperCased); } toUpper(name); Why doesn't this code work? – emiratli Jan 13 '21 at 19:31
  • Well, `function toUpper(text) { console.log(upperCased); }` makes no sense because `upperCased` doesn't exist in that version of the code. You _could_ write `function toUpper(text) { console.log(text.toUpperCase()); }` if you wanted to shorten it. – ADyson Jan 13 '21 at 19:45
  • Or you could just forget the toUpper function entirely (because it doesn't add any functionality beyond what toUpperCase already offers) and write `const name = "asus computer"; console.log(name.toUpperCase());`. Again, whether you declare a variable or not often depends on whether you're intending to use it more than once. – ADyson Jan 13 '21 at 19:47
  • P.S. as a point of terminology, earlier you said `Then define another one and make its value a function`...but actually we make don't make its value a function, we make it _the value returned by executing a function_ . You _can_ assign a (reference to) a function as a variable, but that's not a topic for beginners. However I just wanted to point out that using the correct language is important when describing code, otherwise people can mis-understand. – ADyson Jan 13 '21 at 19:52
  • @ADyson thank you so much! It was my first question as a "developer" so I might have confused you a bit. Sorry! – emiratli Jan 13 '21 at 20:08

0 Answers0