,

JavaScript Variables: A Beginner’s Tutorial – Part 1

Posted by






Variables in JavaScript

Variables in JavaScript

JavaScript tutorial for beginners – Part 1

Variables in JavaScript are used to store data. They are like containers that hold values that can be used in the program. In JavaScript, variables can hold various types of data such as numbers, strings, and objects.

Declaring Variables

In JavaScript, variables are declared using the ‘var’, ‘let’, or ‘const’ keyword. For example:

var x = 10;

let name = 'John';

const PI = 3.14;

The ‘var’ keyword is used to declare a variable that can be reassigned, while the ‘let’ keyword declares a variable that can be reassigned. The ‘const’ keyword is used to declare a variable that cannot be reassigned.

Using Variables

Once a variable is declared, it can be used to store and manipulate data. For example:

var a = 5;

var b = 10;

var sum = a + b;

console.log(sum); // Output: 15

Variables can also be used to store strings:

var greeting = 'Hello, world!';

console.log(greeting); // Output: Hello, world!

Scope of Variables

Variables in JavaScript have function-level scope. This means that a variable declared inside a function is only accessible within that function. For example:

function myFunction() {

var x = 50;

console.log(x); // Output: 50

}

console.log(x); // Error: x is not defined

Conclusion

Variables are an important concept in JavaScript and are used to store and manipulate data in a program. Understanding how to declare and use variables is crucial for writing effective JavaScript code.


0 0 votes
Article Rating
14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dipali Mali
7 months ago

please make videos in marathi for better understanding for
marathi students …

megha lake
7 months ago

Nice

pratu
7 months ago

Very nice, thanks for sharing

mayuri hajare
7 months ago

Very nice

kalyan Narute
7 months ago

Very helpful

poonam
7 months ago

Very useful,helpful to us

Jes
Jes
7 months ago

thanks for sharing

codeFirst Academy
7 months ago

must watch

amber
7 months ago

good man..thanks for sharing

eddie
7 months ago

easy to learn as a freshers

Ankita Khangar
7 months ago

nice explanation.

AKANKSHA ASUTKAR
7 months ago

thanks for sharing

Sanket
7 months ago

thanks

voidChetan2
7 months ago

Keep it up..