JavaScript Variable Keyword


JavaScript Variable Keyword

JavaScript has variables, just like many other programming languages. Varying can be viewed as a type of container with a name. Using these containers, you can store data and then refer to it using the container name.

A variable must first be declared before being used in a JavaScript programme. It declares variables using the var keyword. Variable initialization refers to the process of storing a value in a variable. It is possible to initialize a variable at the time of creation or later when it is needed.

Languages such as JavaScript are not typed. Essentially, this means that a JavaScript variable can contain any data. Unlike many other languages, JavaScript does not need you to specify the kind of value a variable will hold when it is declared. JavaScript automatically detects when a variable's value type changes throughout the execution of a programme.

Names of JavaScript variables

JavaScript variable naming should adhere to the following principles.

  1. As a variable name, you should not use any JavaScript reserved keywords. Break or boolean variable names, for example, are not proper names.
  2. The first character of a JavaScript variable name should not be a number (0-9). This means that they have to begin with a letter or a dash. However, the proper name for the variable is _123test, rather than 123test.
  3. The case is taken into account when naming variables in JavaScript. Name and name, for example, are two separate variables.

Example:

<html> <body> <h2>Knowledge2life</h2> <p id=”demo”></p> <script> var x = 5; var y = 6; var z = x + y; document.getElementById(“demo”).innerHTML = “The value of z is: “ + z; </script> </body> </html>

OUTPUT:

Knowledge2life

The value of z is:11