akotch
akotch

Reputation: 215

No need to declare types in parameters in javascript?

I noticed while learning javascript that it does not require you to declare types in the parameter of a function like java does. How does the compiler know what type is passed? is there any type checking? Lets say my function handles numbers instead of strings and I pass a string?

Also normally in javascript do you not need to specify in the parameters that you are passing a function? Again how does the compiler know?

function invokeAdd(a,b){
    return a()+b();
}

Upvotes: 3

Views: 3577

Answers (3)

Stack Tracer
Stack Tracer

Reputation: 1028

JavaScript doesn't need to know what type of parameters you are passing.

JS is a "dynamically typed" language, which means that it can figure out types as you go.

In the example you gave, passing a non-function type in would result in an error, because the two types are not functions. (However, JS is perfectly fine doing all kinds of other conversions)

A couple of examples are below:

console.log("hello"+1);
console.log("2"+1);
console.log(15+true);
let a = (x)=>x+3;
console.log(a);
console.log(a(1));
console.log(a+1);

If you desire static type checking in JS; Typescript and Flow are alternative languages which transpile into JS.

Upvotes: 1

Isaac
Isaac

Reputation: 12874

To target your specific code blocks, kindly take a look at the below code

function sumA()
{
  return 2;
}

function sumB()
{
  return 3;
}

function invokeAdd(a,b){
  return a()+b();
}

console.log(invokeAdd(sumA, sumB)); //5

The above code will resulted in 5. Let's break them into steps.

When we first invoke the function invokeAdd, we passed in 2 variable sumA to be first parameter which is a and sumB as second parameter which is b. Now invokeAdd becomes something as below:

return sumA() + sumB() 

and hence return 5.

Now let's take another example where we switch the paramter to numbers.

console.log(invokeAdd(1, 2));

We now invoke the function invokeAdd and passing in 2 parameters, 1 and 2 respectively and our invokeAdd has turned into below

return 1() + 2();

Because 1 is not a function and we have a parenthesis () beside it, JS engine will throw an error. () means execute the function

TypeError: a is not a function

If you wish to learn more about types in javascript, You-don't-know-JS is a very good book with detailed explainations

Upvotes: 0

Kurt
Kurt

Reputation: 1897

JavaScript is not a compiled language; It's an interpreted language. So no static type-checking.

To answer one of your specific questions:

Lets say my function handles numbers instead of strings and I pass a string?

You can try this out quite easily:

let number = 1;
let string = "STRING";

let addition = number + string;

console.log(addition);

Notice that all this does is concatenate the two variables. So 1 + "STRING" is 1STRING (and the result is a string type).

It's also worth noting, that variables do not have types but values do. In the above code, addition could be set to anything. You could set it as a string on one line and then a number on another and no error will be thrown (until you try to do something invalid with a number or a string). If you want to check the value of a variable before performing some action on it, you can do something like typeof addition === "string" (for example, to see if the value of the addition variable is a string).

Upvotes: 1

Related Questions