js q&a
js q&a
define let
● ‘Let’ is a keyword used to declare variables.
● Variables declared with ‘let’ are limited in scope to the expression in
which they are declared.
● This means they are not accessible outside of their containing block.
function example2() {
console.log(y); // Error: Cannot access 'y' before
initialization
let y = 20;
console.log(y); // Output: 20
}
example2();
2. define var
● Variables declared with ‘var’ have global scope, depending on
where they are declared.
● Variables declared with ‘var’ are hoisted on top of their containing
function.
● This means they are accessible throughout the entire function
function example() {
console.log(x); // Output: undefined
var x = 10;
console.log(x); // Output: 10
}
example();
3. define const
● ‘Const’ is a keyword used to declare constants.
● Value cannot be changed or reassigned after initialization.
● They are limited in scope to the block, statement, or expression in
which they are declared.
const PI = 3.14159;
console.log(PI); // Output: 3.14159
5. define loops
● Used to execute a block of code repeatedly until the certain condition
is met.
● They provide a way to automate repetitive tasks and repeat over
collections of data.
● They are of 3 types. For loop, while loop, do while loop.
● For Loop: Executes a block of code a specified number of times.
● While Loop: Executes a block of code while a specified condition is
true.
● Do-While Loop: Executes a block of code once, then repeats the loop
as long as a specified
button.addEventListener('dblclick', function() {
console.log('Button double clicked!');
});
● Key Press Event Listener: This listens for a key press event on the
document and executes a function when a key is pressed.
document.addEventListener('keypress', function(event) {
console.log('Key pressed: ' + event.key);
});
17. define switch case
Switch statements in JavaScript provide a way to execute different
blocks of code based on the value of a variable or expression.
switch (day) {
case "Monday":
console.log("It's the start of the week.");
case "Tuesday":
console.log("It's Tuesday!");
case "Wednesday":
console.log("It's midweek.");
default:
console.log("It's some other day.");
}
Difference:-
● Backtick strings:
➢ created using backticks (``),
➢ allow embedding expressions and variables directly within
the string using ${} syntax.
➢ Backtick strings allow easy inclusion of single and double
quotes within the string without escaping.
● Normal strings:
➢ created using single quotes (') or double quotes (").
➢ require concatenation or string insertion methods like + to
include variables.
➢ require escaping single or double quotes with backslashes.