Skip to main content

"All about Variables"


 "In the realm of JavaScript's code,
Variables hold a powerful mode."


In JavaScript, variables are used to store data values. 

Variable Declaration:

In JavaScript, you can declare variables using the var, let, or const keywords.
Already discussed in the previous blog ( Link to it ).







1. Variable Naming:

  • Variables can have a wide range of names, but they must follow certain rules:
  • They can contain letters, digits, underscores, or the dollar sign.
  • The first character must be a letter, an underscore, or a dollar sign.
  • Variables are case-sensitive (myVariable and myvariable are considered different variables).
  • It's a good practice to use descriptive names that reflect the purpose of the variable.
JavaScript has a set of reserved keywords that have predefined meanings and cannot be used as identifiers (such as variable names or function names) in your code.




2. Variable Assignment:

  • You can assign a value to a variable using the assignment operator (=).
  • The assigned value can be a number, string, boolean, array, object, or any other valid JavaScript data type.

3. Variable Reassignment:

  • Once a variable is declared, you can update its value by assigning a new value to it.
  • Variables declared with let can be reassigned with a new value, while variables declared with const are read-only and cannot be reassigned.

4. Variable Scope:

  • The scope of a variable determines where it is accessible within your code.
  • Variables declared with var have function-level scope, meaning they are accessible within the function in which they are declared or globally if declared outside of any function.
  • Variables declared with let and const have block-level scope, meaning they are accessible only within the block of code they are declared in (e.g., inside a loop or an if statement).

Supplementary Reading:


In JavaScript, variables are stored in memory during the execution of a program. The memory location where a variable's value is stored depends on the type of variable and its scope. Let's discuss how variables are stored in memory in JavaScript:

Primitive Data Types:

Variables that hold primitive data types (such as numbers, strings, booleans, null, and undefined) store the actual value directly in memory.

When you declare a variable and assign a primitive value to it, the value is stored in memory at the variable's memory location.




Comments

Popular posts from this blog

"A Beginner's Guide to the Fundamentals"

  How to run JavaScript code? To write JavaScript code using Visual Studio Code (VSCode), follow these steps: 1. Install Visual Studio Code: Download and install Visual Studio Code from the official website: https://code.visualstudio.com/ 2. Set Up a Project Folder: Create a folder on your computer where you'll store your JavaScript files. Open Visual Studio Code and select "Open Folder" to open your project folder. 3. Create a JavaScript File: Inside your project folder, right-click and choose "New File" to create a new file. Give the file a name with the .js extension (e.g., index.js). 4.Write JavaScript Code: Open the JavaScript file in Visual Studio Code. Start writing your JavaScript code in the file. 5. Running JavaScript Code: To execute the JavaScript code, you have a few options: Open the integrated terminal in Visual Studio Code using Ctrl+ (backtick) and runnode script.js` to run the script using Node.js. Install Node.js from here To print "Hello...

"A Simple Explanation : Data Types and Operators"

Data Types JavaScript has several built-in data types, which are used to represent different kinds of values.    Undefined: It means that a variable has been declared but doesn't have a value yet.    Null: It represents the intentional absence of any value.    Boolean: It has only two values, either "true" or "false". It's used for making decisions.    Number: It represents any numeric value, like whole numbers or decimals.    String: It represents a sequence of characters, like words or sentences. It is enclosed in quotes.    Symbol: It's a unique and immutable value often used as a special identifier.   Object: It's a collection of properties and methods, like a container that holds related information. Array: It's a list of values stored in a specific order.    Function: It's a reusable block of code that performs a specific task.   Remember, these are the basic data types, and JavaScript al...