can i write this function without brackets in variable? | Sololearn: Learn to code for FREE!

0

can i write this function without brackets in variable?

function print(x) { return x-7 } var print1=print console.log(print1(3))

9/24/2021 6:08:44 AM

Aniket Ganguly

14 Answers

New Answer

+4

The only way to write the function "print" without parenthesis is to use a lambda function. This only works when there is a single argument for the function. If there is more than one argument they must be placed in parenthesis and separated by commas. Even after all that you still have to call the function using parenthesis... //single argument const print = x => x - y; print1 = print; console.log(print1(3)); //multiple arguments const print = (x, y) => x - y; print1 = print; console.log(print1(3, 7)); This is a contrived and obfuscated use of lambda functions and has no value whatsoever in demonstrating the use of lambda functions or good coding practices. If you are going to call a function after defining it there is no point in using a lambda function. "Any fool can write code a computer can understand. Good programmers write code that humans can understand." -- Martin Fowler

+2

Why ask a question with an obvious answer?

+2

Vasiliy Obvious question? I suppose you have an obvious answer?

+2

Vasiliy Already done

+1

Ipang print1

0

I didn't get your question ... Do you want to call print1() or print() ??? * The snippet in Description had been modified from the original.

0

Ipang I'm surprised that you don't have it. 😕

0

Vasiliy That means you do then?

0

Ipang I do not know English well and did not understand your question, reformulate the question.

0

Vasiliy You said you were surprised I don't have any answer to the OP's question. Do you have the answer?

0

Ipang open javascript and make sure the Aniket Ganguly code works.

0

It's written as const print1 = x => x - 7;

0

As long as javascript is concerned in this discussion. 1. arrow function 2. lambda function 3. lambda expresssion 4. Anonymous function Are markedly different things unlike it is in python and other languages. A lambda function comes like this (() => ({foo: 1}))() An arrow function comes like this () => {}; An anonymous function comes like this function() {....} a traditional function comes like this function name(){...} 1. Not every anonymous function is a lambda function, and not every lambda function is an arrow function and/or anoynmous. The use of arrow function is not valueless as described by Martin Taylor, infact i use them the most. There's absolutely no difference between a named arrow function and traditional function in javascript apart from accessing the `this` context.

-1

Aniket Use of arrow function may help const print = ( x ) => x - 7; let print1 = print; console.log( print1( 49 ), print( 49 ) );