Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

What are the ways in which variables created in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

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;

LRM_EXPORT_207556595493866_20190724_1939

Q

2

What are the different scopes of JavaScript variables?

LRM_EXPORT_207556595493866_20190724_1939

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.

LRM_EXPORT_207556595493866_20190724_1939

Q

3

What are undefined variables in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

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.

LRM_EXPORT_207556595493866_20190724_1939

Q

4

What are undeclared variables?

LRM_EXPORT_207556595493866_20190724_1939

Ans

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

LRM_EXPORT_207556595493866_20190724_1939

Q

5

Explain the use of 'const' keyword in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

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

LRM_EXPORT_207556595493866_20190724_1939

Q

6

Explain the use of 'let' keyword in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

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

LRM_EXPORT_207556595493866_20190724_1939

Q

7

What are the rules for creating a legal JavaScript identifier?

LRM_EXPORT_207556595493866_20190724_1939

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.

LRM_EXPORT_207556595493866_20190724_1939

Q

8

Write a note on Global Variables. How are they declared in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

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.

LRM_EXPORT_207556595493866_20190724_1939

Q

9

What is Variable Typing?

LRM_EXPORT_207556595493866_20190724_1939

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.

LRM_EXPORT_207556595493866_20190724_1939