To create a variable in JavaScript, you can use the var
, let
, or const
keyword followed by the variable name. Here's the syntax for each:
var
: It is the older way of declaring a variable, but it is still commonly used.var variableName = value;
let
: Introduced in ES6, let
is block-scoped and allows you to declare a variable that can be reassigned later.let variableName = value;
const
: Also introduced in ES6, const
is block-scoped and creates a read-only variable that cannot be reassigned.const variableName = value;
Here's an example of creating a variable:
var message = "Hello, world!";
let count = 5;
const PI = 3.14;
In the example above, the variable message
is created using var
and assigned the value "Hello, world!". The variable count
is created using let
and assigned the value 5. The variable PI
is created using const
and assigned the value 3.14.