Skip to main content

Command Palette

Search for a command to run...

JAVASCRIPT BEGINNER-Day-02

"Vital JavaScript Skills: What Beginners Need to Learn"

Published
•3 min read•View as Markdown
JAVASCRIPT BEGINNER-Day-02

console.log(“CONNECT AND LEARN WITH ME");

Word count——497 words

Paragraphs—-20 paragraphs

Read time——3 minutes

-II- All ABOUT Javascript Variables

## What is a Variable?

ANS—-In programming, a variable is like a container used to store data. Think of it as a labeled box where you can keep information, such as a number, a word, or any other type of data, so you can use it later in your code.

var ,let ,const —> remember this three names

## How to Declare a Variable ?

In JavaScript, you declare a variable using keywords like var, let, or const. Each one has different characteristics:

  1. var:

    • It was used in older versions of JavaScript and is still supported.

    • Variables declared with var can be updated or re-declared within the same scope.

    • They have function scope, meaning they are accessible throughout the entire function they're declared in.


    var name = "John"; // Declares a variable named 'name' with the value "John"
  1. let:

    • Introduced in ES6 (modern JavaScript).

    • Allows you to update the value of a variable, but you cannot re-declare it in the same scope.

    • It has block scope, meaning it is accessible only within the block {} it is declared in.

    let age = 25; // Declares a variable named 'age' with the value 25
    age = 26; // You can update its value
  1. const:

    • Also introduced in ES6.

    • It is used for variables whose value should not change (constant).

    • You cannot reassign a new value to a const variable.

    • It is block-scoped, like let.

    const pi = 3.14159; // Declares a constant variable named 'pi' with the value 3.14159
    // pi = 3.14; // This will throw an error because you can't reassign a value to a constant

Variable Naming Rules

  • Variable names can include letters, digits, underscores (_), and dollar signs ($).

  • They cannot start with a digit.

  • Variable names are case-sensitive, which means age, Age, and AGE are considered different variables.

let age = 30;
let Age = 25;
console.log(age); // Output: 30
console.log(Age); // Output: 25
  • It’s a good practice to use meaningful names for your variables to describe what they represent, like userName or totalPrice.

    • 
        let name1 = "John"; // Valid
        let _age = 25; // Valid
        let $salary = 5000; // Valid
        let 1stName = "Alice"; // Invalid: Starts with a digit
      

VARIABLE SCOPES

  • BLOCK scoped

  • FUNCTION scoped

  • GLOBAL scoped

THIS IS IT…… THE BASIC INTRODUCTION


...let's meet on day 03!

In this article, you'll learn about JavaScript variables, which are used to store data in your code. We'll explore how to declare variables using `var`, `let`, and `const`, highlighting their different scopes and characteristics. The article also covers variable naming rules, emphasizing the importance of meaningful names, and briefly touches on the concepts of block, function, and global scopes.

THANK YOU...

linkedin github