7
7
75db772020-03-15 23:55:57
typescript
75db77, 2020-03-15 23:55:57

How to make other parameters not required?

I need to create a function that takes three parameters. In this function, only the first parameter is required, and the second and third may not be passed.

How to implement it? Give me a small example if possible...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Suha, 2020-03-16
@75db77

function test(a, b=null, c=null) {
    console.log(a);
     if(b) console.log(b);
     if(c) console.log(c);
}

test(1) // консоль: 1
test(1, 2) // консоль: 1, 2

A
Aetae, 2020-03-16
@Aetae

Are we talking about typescript?

function test (a: number, b?: number, c?: number) {
  console.log(a);
  if (b) console.log(b);
  if (c) console.log(c);
}

test(1) // консоль: 1
test(1, 2) // консоль: 1, 2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question