top of page

JavaScript Class Notes: Variables

Updated: Oct 16, 2022

Mobiprep has created last-minute notes for all topics of JavaScript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on JavaScript.



Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

JavaScript Notes: Variables


Question 1. What are the ways in which variables created in JavaScript?

Variables act as the containers for storing data values. In JavaScript, variables are declared by using three methods:

  1. var

  2. let

  3. Const

The 2015 version of JavaScript allows the use of ‘const’ to define a variable that cannot be reassigned and the use of ‘let’ to define a restricted scope (explained later).


Example:

var x= 5;   
var y=6;

 

Question 2. What are the different scopes of JavaScript variables?

Scope determines the accessibility of variables. In JavaScript there are two types of scopes:

  1. Local Scope

  2. Global scope

  • Local Scope :- Variables declared within a JavaScript function are called local variables. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions. Local variables have local/function scope.

  • Global scope :- Variable declared outside a function becomes a global variable. Global variables have global scope. All scripts and functions on a web page can access it.

 

Question 3. What are the undefined variables in JavaScript?

Variables are often declared without a value; this value can be something to be calculated or proved or can be user input. This is called undefined variable.

 

Question 4. What are undeclared variables?

Undeclared variables occur when we try to access any variable that is not initialized or declared earlier using var or const keyword.

 

Question 5. Explain the use of 'const' keyword in JavaScript.

As mentioned above, in 2015 version of JavaScript, two variable keywords were introduced- ‘const’ and ‘let’.

These both have same function, except const cannot be reassigned. JavaScript const variables must be assigned a value when they are declared. Redeclaring a JavaScript var variable is allowed anywhere in a program but with const doesn’t allow it.

Example:

const x= 2     //Allowed
const x= 3    //Not Allowed 

 

Question 6. Explain the use of 'let' keyword in JavaScript.

‘let’ allows JavaScript to reassign values to variables. This has global scope and function scope; hence the variables can be global and local.

Example:

{  let x= 2; }    //// x can NOT be used here
 


Question 7. What are the rules for creating a legal JavaScript identifier?

  • Identifiers are names.

  • In JavaScript, identifiers are used to name variables (and keywords, and functions, and labels).

  • The rules for legal names are much the same in most programming languages.

  • In JavaScript, the first character must be a letter, or an underscore (_), or a dollar sign ($).

  • Subsequent characters may be letters, digits, underscores, or dollar signs.

  • First character cannot be numbers.

 

Question 8. Write a note on Global Variables. How are they declared in

JavaScript?

A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function.

Declaring a global variable:

To declare JavaScript global variables inside function, you need to use window object.

Example:

window.value= 50;

Now it can be declared inside any function and can be accessed from any function. The window object represents the browser's window.

 

Question 9. What is Variable Typing?

JavaScript is a very loosely typed language; the type of a variable is determined only when a value is assigned and can change as the variable appears in different contexts. Usually, you don’t have to worry about the type; JavaScript figures out what you want and just does it.




 

Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.
Free Practice Tests
Top Placement Interview Questions
Top Placement MCQ's Questions

 

549 views0 comments

Recent Posts

See All

Opmerkingen


bottom of page