how to declare a variable in javascript in javascript

In JavaScript, there are two ways to declare a variable - using the var keyword or the let keyword.

Here is an example of declaring a variable using the var keyword:

index.tsx
var num = 10;
14 chars
2 lines

In this example, we declared a variable named num and assigned it a value of 10.

Here is an example of declaring a variable using the let keyword:

index.tsx
let greeting = "Hello World";
30 chars
2 lines

In this example, we declared a variable named greeting and assigned it a value of "Hello World".

Note that the difference between var and let lies in their scoping rules. var has function scope while let has block scope.

gistlibby LogSnag