Saturday, 24 June 2023

Variable define in JavaScript

Variable define in JavaScript
There are four ways to declare variables in JavaScript:
  • Automatically
  • Using var
  • Using let
  • Using const

They are automatically keywords declared:
x = 5;
y = 6;
z = x + y;

They are var keywords declared:
var x = 5;
var y = 6;
var z = x + y;

They are let keywords declared:
let x = 5;
let y = 6;
let z = x + y;

They are const keywords declared:
const x = 5;
const y = 6;
const z = x + y;

They are Mixed keywords declared:
const price1 = 5;
const price2 = 6;
let total = price1 + price2;

Note: Mostly in trend let and const keywords were used in JavaScript which is added in 2015.

SetVarJavaScript.html
<html>
   <body>
         <script type="text/javascript">
         var n=prompt("Enter age.........>");
            var age = 15;
            if( age > n )
{
               document.write("<b>Qualifies for driving</b>");
            }
            else{
               document.write("<b>Does not qualify for driving</b>");
            }
      </script>
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

0 comments

Post a Comment