DEV Community

Narmatha
Narmatha

Posted on

JAVASCRIPT VARIABLES INTERVIEW QUESTION

WHAT IS VARIABLES IN JAVASCRIPT

Variables in JavaScript are used to store data values. They can be declared in different ways depending on how the value should behave.

Variables can be declared using var, let, or const.
JavaScript is dynamically typed, so types are decided at runtime.
You don’t need to specify a data type when creating a variable.

TYPES OF VARIABLES

1. var keyword
var is a keyword in JavaScript used to declare variables and it is Function-scoped and hoisted, allowing redeclaration but can lead to unexpected bugs.

var a = "Hello Geeks";
var b = 10;
console.log(a);
console.log(b);
Enter fullscreen mode Exit fullscreen mode

2. let keyword
let is a keyword in JavaScript used to declare variables and it is Block-scoped and not hoisted to the top, suitable for mutable variables

let a = 12
let b = "gfg";
console.log(a);
console.log(b);
Enter fullscreen mode Exit fullscreen mode

3. const keyword
const is a keyword in JavaScript used to declare variables and it is Block-scoped, immutable bindings that can't be reassigned, though objects can still be mutated.

const a = 5
let b = "gfg";
console.log(a);
console.log(b);
Enter fullscreen mode Exit fullscreen mode

Rules for Naming Variables

1.Variable names must begin with a letter, underscore (_), or dollar sign ($).
2.Subsequent characters can be letters, numbers, underscores, or dollar signs.
3.Variable names are case-sensitive (e.g., age and Age are different variables).
4.Reserved keywords (like function, class, return, etc.) cannot be used as variable names.

Top comments (0)