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.
please make videos in marathi for better understanding for
marathi students …
Nice
Very nice, thanks for sharing
Very nice
Very helpful
Very useful,helpful to us
thanks for sharing
must watch
good man..thanks for sharing
easy to learn as a freshers
nice explanation.
thanks for sharing
thanks
Keep it up..