0

I have just been looking at a tutorial for 3js scrolling animation control. Just saw in the example code that there's this strange function declaration with parameters set up that I've seen before. They look like key value pairs. When I copied and pasted the entire javascript code provided by the tutorial into my IDE and run it, it tells me that the ':' is unexpected. I was just wondering if this a valid format and get some more info how this code works.

function lerp( x: number, y: number, a: number):  number {
        return (1 - a) * x + a * y
    }
  • 2
    That looks like [typescript](https://www.typescriptlang.org/) and not plain JS – Nick Parsons Dec 12 '21 at 10:18
  • Remove the typing from the typescript function to get the usual js function => `function lerp( x, y, a) { return (1 - a) * x + a * y }` – Amila Senadheera Dec 12 '21 at 10:21
  • [Duplicate](//google.com/search?q=site%253Astackoverflow.com+js+colon+in+function+parameters) of [What does colon do in a javascript function parameter](/q/30610997/4642212). – Sebastian Simon Dec 12 '21 at 10:29

2 Answers2

0

This isn't exactly javascript, but typescript.

This syntax lets the interpretor know that x, y and a are supposed to be of type number.

Noam-NS
  • 276
  • 2
  • 5
0

The function is declared in typescript you have to convert it into javascript in order to execute it directly or you can remove the type definations like this it then it will work.

function lerp(x, y, a) {
    return (1 - a) * x + a * y;
}