Variables
In JavaScript, variables are used to store and manage data. They are created using the var, let, or const keyword.
var Keyword
The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behavior.
var x = 10;
Example: In this example, we will declare variables using var.
var a = "Hello Geeks"
var b = 10;
var c = 12;
var d = b + c;
console.log(a);
console.log(b);
console.log(c);
console.log(d);
Output
Hello Geeks 10 12 22
let Keyword
The let keyword is a block-scoped variables. It’s commonly used for variables that may change their value.
let y = "Hello";
Example: In this example, we will declare variables using let.
let a = "Hello learners"
let b = "joining";
let c = " 12";
let d = b + c;
console.log(a);
console.log(b);
console.log(c);
console.log(d);
Output
Hello learners joining 12 joining 12
const Keyword
The const keyword declares variables that cannot be reassigned. It’s block-scoped as well.
const PI = 3.14;
Example: In this example, we will declare the variable using the const keyword.
const a = "Hello learners"
console.log(a);
const b = 400;
console.log(b);
const c = "12";
console.log(c);
// Can not change a value for a constant
// c = "new"
// console.log(c) will show error
Output
Hello learners 400 12
Variables and Datatypes in JavaScript
Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, understanding these concepts is essential for writing effective and efficient code.
Contact Us