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 variableaand initializes it with the value 5.var myVar = 'DeathCode';- Declares a variablemyVarwith a string value. This variable is function-scoped or globally-scoped.const pi = 3.14;- Declares a constant variablepiwith a value of 3.14. This value cannot be changed.{ var b = 7; }- Declares a variablebinside a block. However, sincevaris function-scoped,bis accessible outside the block.console.log("b:", b);- Prints the value ofbto the console.console.log("number:", a, myVar);- Prints the values ofaandmyVarto the console.a = 7;- Updates the value ofato 7.console.log("number:", a);- Prints the updated value ofato the console.console.log("PI:", pi);- Prints the value of the constantpito the console.let length = 6;- Declares a block-scoped variablelengthand initializes it with 6.let breadth = 7, area = length * breadth;- Declares two variables,breadthandarea. Theareais calculated by multiplyinglengthandbreadth.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.,
myVarandmyvarare different). - Use camelCase or underscores for multi-word variable names (e.g.,
myNameormy_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.
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch