0

Hy I am a new to JavaScript. I have done programming with Java. In Java we declares variables with proper type. string, int, char etc. Is there a way to declare variable in JavaScript having proper type instead of let, var etc.

Atif Zia
  • 777
  • 2
  • 11
  • 28
  • 4
    There are no strict types in native JavaScript but you can take a look on TypeScript. – Jax-p Aug 12 '20 at 14:29
  • 1
    Javascript is a dynamically typed language, if you want static typing, take a look at [Typescript](https://www.typescriptlang.org/) or [Flow](https://flow.org/) – Yousaf Aug 12 '20 at 14:31
  • 1
    See [What is TypeScript and why would I use it in place of JavaScript?](https://stackoverflow.com/q/12694530). I'm not voting for a dupe, as it's only one of the possibilities - another alternative is Flow. There are other static typing systems available, as well. I've used Tern.JS in the past and it had nice integrations with some editors. It further took the types from JSDoc, so it worked on plain JS, while TS and Flow need a compilation step. – VLAZ Aug 12 '20 at 14:32

2 Answers2

0

While using javascript stack (javascript, typescript) you are going to use let/const but you can either strictly define the type like:

let a: number = 5;

or leave it to javascript engine

let b = 5;

Eduard
  • 1,768
  • 2
  • 10
  • 14
0

Javascript is a dynamically typed language, the type is assigned by the javascript itself, it has some ways of assigning types to variables, but none natively, they all need some framework. The most famous way is typescript which is a superset of javascript, other way is flow.js.

If you want only check a variable type you can use typeof or === operator. e.g: == check only value, 1 == "1" is true, but === check value and type, 1 === "1" is false.

If you're new to JS I personally recommend focus in learn JavaScript and in future study other frameworks such as Typescript.

useful study links