Javascript Notes
JavaScript
Data Types
Variables
Operators
Functions
Conditional Statements and Loops
Arrays
Objects
DOM Elements
Cookies
Pop Up Box
Exception Handling
Heading
Q
What are the ways in which variables created in JavaScript?
Ans
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;
Q
2
What are the different scopes of JavaScript variables?
Ans
Scope determines the accessibility of variables. In JavaScript there are two types of scopes:
a. Local Scope b. Global scope
a. 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.
b. 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.
Q
3
What are undefined variables in JavaScript?
Ans
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.
Q
4
What are undeclared variables?
Ans
Undeclared variables occur when we try to access any variable that is not initialized or declared earlier using var or const keyword.
Q
5
Explain the use of 'const' keyword in JavaScript.
Ans
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
Q
6
Explain the use of 'let' keyword in JavaScript.
Ans
‘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
Q
7
What are the rules for creating a legal JavaScript identifier?
Ans
a. Identifiers are names.
b.In JavaScript, identifiers are used to name variables (and keywords, and functions, and labels).
c.The rules for legal names are much the same in most programming languages.
d.In JavaScript, the first character must be a letter, or an underscore (_), or a dollar sign ($).
e.Subsequent characters may be letters, digits, underscores, or dollar signs.
f.First character cannot be numbers.
Q
8
Write a note on Global Variables. How are they declared in JavaScript?
Ans
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.
Q
9
What is Variable Typing?
Ans
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.