Variables and constants

In JavaScript, variables are used to store data values, and constants hold values that do not change. There are three ways to declare variables: var, let, and const.

1. Declaring Variables

JavaScript provides two modern ways (let and var) and one older way (var) to declare variables.

1.1 Using var (Old Way, Avoid Using)

  • Can be redeclared within the same scope.
  • Function-scoped (not block-scoped).
  • May lead to unexpected behavior due to hoisting.
var name = "Alice"; 
console.log(name); // Output: Alice
var name = "Bob"; // Redeclaration is allowed
console.log(name); // Output: Bob

🔹 Why avoid var?

Since var does not respect block scope, it can cause unexpected issues in larger programs.

1.2 Using let (Modern & Recommended)

Block-scoped (only available inside {} where declared).

Can be updated but not redeclared in the same scope.

let age = 25;
console.log(age); // Output: 25
age = 30; // Allowed: Updating the variable
console.log(age); // Output: 30
// let age = 40; ❌ Error: Cannot redeclare the variable

🔹 Best practice: Use let for values that may change.

2. Declaring Constants (const)

  • Block-scoped like let.
  • Cannot be reassigned after initialization.
  • Must be initialized when declared.
const PI = 3.14;
console.log(PI); // Output: 3.14
// PI = 3.14159; ❌ Error: Cannot reassign a constant

🔹 Best practice: Use const for values that should not change (e.g., mathematical constants, API keys).

Scope of Variables

JavaScript variables have different scopes depending on how they are declared:

Variable TypeScopeCan be Updated?Can be Redeclared?Hoisted?
varFunction✅ Yes✅ Yes✅ Yes (with undefined)
letBlock✅ Yes❌ No❌ No
constBlock❌ No❌ No❌ No
Qus. 1 : The type of a variable that is volatile is

  1. Volatile variable
  2. Mutable variable
  3. Immutable variable
  4. Dynamic variable
Qus. 2 : Which of the following is not a valid JavaScript variable name?

  1. 5myvalue
  2. myvalue5
  3. Myvalue
  4. None of These
Qus. 3 : How can you add a comment in a JavaScript?

  1. <!--This is a comment-->
  2. //This is a comment
  3. 'This is a comment
  4. #This is a comment
Qus. 4 : How do you declare a JavaScript variable?

  1. variable carName;
  2. var carName;
  3. vari carName;
  4. dim carName;
Qus. 5 : Which statement specify correct difference between var and let keywords in JavaScript ?

  1. var defines a variable while let defines a constant.
  2. There is no such major difference between them.
  3. The value of a variable declared with var can be changed while the value of a variable declared with let cannot be changed.
  4. var defined function scoped variable while let define block scoped variable.
Qus. 6 : Which one of the following is not considered as an error

  1. Division by zero
  2. Missing of semicolons
  3. Syntax error
  4. Both A and B
Qus. 7 : The correct syntax to write "Sky is blue" in JavaScript is

  1. jscript.write("Sky is blue")
  2. document.write("Sky is blue")
  3. print("Sky is blue")
  4. jscript.print("Sky is blue")
Qus. 8 : Which keyword is a must to use when JavaScript code is to run in older browser?

  1. var
  2. let
  3. const
  4. none of the above
Qus. 9 : What is "this" in JavaScript:

  1. keyword
  2. function
  3. Declaration Statement
  4. Data type
Qus. 10 : Which statement specify correct difference between var and let keywords in JavaScript?

  1. var defines a variable while let defines a constant.
  2. There is no such major difference between them
  3. The value of variable declared with let cannot be changed
  4. var define function scoped variable while let define block scoped variable
Qus. 11 : What does the console.log() function do?

  1. Displays a message in an alert box
  2. Prints output to the console
  3. Writes data to a file
  4. Logs an error in the browser console
Qus. 12 : How do you write a comment in JavaScript?

  1. //This is a comment
  2. <!--This is a comment-->
  3. /* This is a comment */
  4. Both A and C
Qus. 13 : For displaying data in JavaScript, we can’t use _

  1. document. write()
  2. console.log()
  3. innerHTML
  4. document.getElementById()
Qus. 14 : For testing we should use

  1. document. write()
  2. console.log()
  3. window. alert()
  4. innerHTML
Qus. 15 : While working on a JavaScript project, in your JavaScript application, which function would you use to send messages to users requesting for text input?

  1. Display()
  2. Prompt()
  3. Alert()
  4. Confirm()

Programs

JavaScript code that calculates the squares and cubes from 0 to 10

View Solution


Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad