
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Function Expression as a Constant Value in JavaScript
If the const is used in a program, and if you try to reassign the value to const variable then an error will arise.
Let’s say the following is our const variable −
const result = (first, second) => first * second;
Now, we will try to reassign a value to the const variable and an erro can be seen in the output.
Example
Following is the code −
const result = (first, second) => first * second; result = first => first =first*10; console.log(result(10,20));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo284.js.
Output
This will produce the following output on console −
Error is visible “Assignment to constant variable” −
PS C:\Users\Amit\javascript-code> node demo284.js C:\Users\Amit\javascript-code\demo284.js:2 result = first => first =first*10; ^ TypeError: Assignment to constant variable. at Object.<anonymous> (C:\Users\Amit\javascript-code\demo284.js:2:8) at Module._compile (internal/modules/cjs/loader.js:1133:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10) at Module.load (internal/modules/cjs/loader.js:977:32) at Function.Module._load (internal/modules/cjs/loader.js:877:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47
On commenting the below line, you will get the correct result −
// result = first => first =first*10;
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo284.js 200
Advertisements