DEV Community

Cover image for JS Test #6: Variable number of arguments
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Edited on • Originally published at learn.coderslang.com

6 1

JS Test #6: Variable number of arguments

code-snippet
true or false? That is the question...

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JS, all functions have access to the internal arguments array that holds all arguments that were passed into the function.

We can access the elements of this array by index, thus expecting that both regularFunction and arrowFunction will return true.

The only issue is that arrow functions don’t have access to the arguments array.

There might be two separate outcomes in line 8. Most likely you’ll see the message ReferenceError: arguments is not defined. However, there also might be a different scenario. For example, if you run this code in Node.js, arguments[2] is likely to be evaluated to something like

Module {
  id: '.',
  path: '/workdir_path',
  exports: {},
  parent: null,
  filename: '/workdir_path/scriptName.js',
  loaded: false,
  children: [],
  paths: [
    '/node_modules'
  ]
}
Enter fullscreen mode Exit fullscreen mode

In which case, we’ll see false logged to the screen as 3 is not equal to the object described above.


ANSWER: false or ReferenceError will appear in the console depending on the execution environment

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay