JS Variables

In JavaScript, variables are containers for storing data values. They can hold different types of data, such as numbers, strings, objects, arrays, and more.


JavaScript Variables can be declared in 4 ways:


  • Using var
  • Using let
  • Using const


Example (Var)
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<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>

Try it yourself

Example (let)
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
let x = 5;
let y = 6;
let z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>

</body>
</html>

Try it yourself

Example (const)
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Variables</h1>
<p>In this example, x, y, and z are variables.</p>
<p id="demo"></p>
<script>
const x = 5;
const y = 6;
const z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>

</body>
</html>

Try it yourself

JavaScript Identifiers


In JavaScript, identifiers are names used to identify variables, functions, arrays, objects, or any other user-defined items. They must follow specific rules to be valid.


The general rules for constructing names for variables (unique identifiers) are:

Must begin with a letter, underscore (_), or dollar sign ($): Names must begin with a letter.

Can contain letters, digits (0-9), underscores (_), or dollar signs ($): after the first character: Names are case sensitive (y and Y are different variables).

Names are case sensitive (y and Y are different variables).

Reserved words (like JavaScript keywords) cannot be used as names.


Note

Some of reserved words are

 

-   break, case, catch, class, const, continue, debugger, default, delete, do, else, export, extends, finally, for, function, if, import, in, instanceof, let, new, return, super, switch, this, throw, try, typeof, var, void, while, with, yield



Examples of Valid and Invalid Identifiers:


       1. Valid


  • let myVar
  • let _result
  • let $currency
  • let age12
  • let this_is_valid
  • let café


      

      2. Invalid


  • let 1number;   // Cannot start with a number
  • let my-var;    // Hyphen is not allowed
  • let let;       // "let" is a reserved keyword
  • let function;  // Reserved keyword

Best Practices:

Use meaningful and descriptive names for identifiers to make your code more readable.

Stick to camelCase for variable and function names (myVariableName).


Use uppercase for constants (MAX_VALUE).


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.