Variables & Comments in JavaScript πŸš€ | JavaScript Journey | In Hindi | Death Code - DeathCode

Variables in JavaScript

In JavaScript, variables are used to store data values. There are three ways to declare variables:

  • var - A function-scoped or globally-scoped variable.
  • let - A block-scoped variable that can be updated but not re-declared within the same scope.
  • const - A block-scoped variable that cannot be updated or re-declared.

Example of Variable Declarations

let a = 5;
var myVar = 'DeathCode';
const pi = 3.14;
{
    var b = 7;
}
// this is printing of b in console
 
/*
alkjsdf;lkasd
fasd ja
; gja
d
*/
 
console.log("b:", b);
console.log("number:", a, myVar);
a = 7;
console.log("number:", a);
console.log("PI:", pi);
 
let length = 6;
let breadth = 7, area = length * breadth;
 
console.log(area);
 
// Rules for variables
// 1. space is not allowed ($)
// 2. camelCase, underScore'_'=>(myName,my_name)
// 3. Can't use number at starting a var name (Not Allowed: let 4TI=99)

Explanation of the Code

  • let a = 5; - Declares a block-scoped variable a and initializes it with the value 5.
  • var myVar = 'DeathCode'; - Declares a variable myVar with a string value. This variable is function-scoped or globally-scoped.
  • const pi = 3.14; - Declares a constant variable pi with a value of 3.14. This value cannot be changed.
  • { var b = 7; } - Declares a variable b inside a block. However, since var is function-scoped, b is accessible outside the block.
  • console.log("b:", b); - Prints the value of b to the console.
  • console.log("number:", a, myVar); - Prints the values of a and myVar to the console.
  • a = 7; - Updates the value of a to 7.
  • console.log("number:", a); - Prints the updated value of a to the console.
  • console.log("PI:", pi); - Prints the value of the constant pi to the console.
  • let length = 6; - Declares a block-scoped variable length and initializes it with 6.
  • let breadth = 7, area = length * breadth; - Declares two variables, breadth and area. The area is calculated by multiplying length and breadth.
  • console.log(area); - Prints the calculated area to the console.

Rules for Creating Variables

  • Variable names must begin with a letter, underscore (_), or dollar sign ($).
  • Spaces are not allowed in variable names.
  • Variable names can include letters, numbers, underscores, and dollar signs.
  • Variable names are case-sensitive (e.g., myVar and myvar are different).
  • Use camelCase or underscores for multi-word variable names (e.g., myName or my_name).
  • Variable names cannot start with a number (e.g., let 4TI = 99; is not allowed).
  • Avoid using reserved keywords (e.g., let, const, function, etc.) as variable names.
Β© 2024 DeathCode. All Rights Reserved.