#Why can I declare the string variable inside a class but I can't inside a function?

20 messages · Page 1 of 1 (latest)

kind bough
#

can someone explain, coming from java learning angular. this annoys me

misty sinew
#

You can do type a variable inside a function

#

that error show because you are not declare your variables

#

use let or const

#
class a {
  private x: number;
  test() {
    let num: number = 1
  }
}
kind bough
#

okay, is let a requirement inside a function? because i can initialized it outside the function.

kind bough
misty sinew
#

you said you learn Java right?

kind bough
#

it uses defaults?

misty sinew
#

class in typescript is more or less the same with java class

#

properties typing is more or less the same with java

in java you do type variableName, in typescript you do variableName: type

#

but inside a function, it is not a class anymore, but classic javascript

kind bough
#

I see

misty sinew
#

you declare a variable with let or const

#

in java you dont use any keyword to declare a variable, so maybe that's why you are confused

kind bough
#

inside a function, I need to add let even though i specified its type? is this correct?

misty sinew
#

well yes, but there are no correlation between let and typing.

let is used to declare a variable

#

so you can do just

let student;