0% found this document useful (0 votes)
7 views

js q&a

Uploaded by

anjanashri108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

js q&a

Uploaded by

anjanashri108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

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

// Attempting to reassign a constant will result in an


error
PI = 3.14; // Error

4. log statements (eg. string + number)


● log statements are used to output information to the console.
● They are commonly used to display messages to the developer.
● The most commonly used log statement is console.log( ) which
outputs a message to the console.

let name = "John";


let age = 30;

console.log("Hello, " + name + "! You are " + age + "


years old."); // Hello John! You are 30 years old.

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

6. purpose of diff. Loops


● For loop
➢ Used when the number of replications is known by a
condition.
➢ Provides a compact syntax for initialising, testing, and
updating loop variables.
● While loop
➢ Used when the number of repetitions is not known
beforehand and depends on a condition.
➢ Executes the loop as long as the specified condition is true.
● Do while loop
➢ Similar to the while loop but the code block is executed at
least once before checking the condition.
➢ Used when we want to execute the loop body at least once,
regardless of the condition.
7. why do we use for loop, while loop, do-while
loop. (ref the prev ans)

8. diff. loop, while loop, do-while loop (ref the prev


ans)

9. diff. =, ==, ===


● Use = for assignment.Used to assign a value to a variable.
let i = 0;
● Use == for loose equality comparison with type coercion. Performs
type coercion, which tries to convert the operands to the same type
before comparison. 0 =='0' // true
● Use === for strict equality comparison without type coercion. Does
not perform type coercion, so the values must be of the same data
type to be considered equal. 0 === '0' // false

10. define DOM


● DOM stands for Document Object Model.
● It is a programming interface that allows us to create, change, or
remove elements from the document.
● We can also add events to these elements to make our page more
dynamic.

11. what is javascript


● JavaScript is a high-level, interpreted programming language primarily
used for making web pages interactive.
● It was created to add dynamic and interactive elements to web pages,
allowing them to respond to user actions
● JavaScript can be embedded directly into HTML code and executed by
web browsers, making it an essential component of web development.
12. why do we use javascript why not python
● We use JavaScript because:
➢ It's the primary language for web development, making web
pages interactive.
➢ It's used with popular frameworks like React and Angular for
building modern web apps.
➢ It's essential for creating dynamic and responsive user
interfaces.
➢ It's widely supported by browsers and has a large community
of developers.

● We might not choose Python because:


➢ It's not built for client-side web development like JavaScript.
➢ While it's great for tasks like data analysis and machine
learning, it's not as suited for front-end web development.

13. importance of javascript


● JavaScript makes web pages interactive and dynamic.
● It allows for client-side scripting, reducing server load and
improving user experience.
● It's used with popular frameworks like React and Angular for
building modern web apps.
● JavaScript builds rich user interfaces and responsive web
applications.
● It is supported by a large and active developer community.

14. why do we use DOM


● It allows to access, manipulate, and update the content, structure,
and style of web pages.
● It enables dynamic and interactive web development by allowing
scripts to respond to user actions, events, and changes in the
document.
● It enables the development of web applications with rich
functionality and responsive design,
● It forms the foundation for modern web development
15. how can we assign or join documents & DOM
with javascript
● Accessing Elements: Select elements from the DOM based on their
ID, class, or CSS selector.
let myElement = document.getElementById('myElement');

● Manipulating Elements: Once selected an element, we can


manipulate its properties, attributes, or content
myElement.innerHTML = 'New content';
myElement.style.color = 'red';

● Creating New Elements: we can create new elements and include


them to the DOM
let newElement = document.createElement('div');
newElement.textContent = 'New element';
document.body.appendChild(newElement);

16. what are events ( like click events, double click


events, key press events )
● Event listeners are functions that are executed in response to a
specific event occurring on an HTML element.
● They "listen" for events to happen and then execute a callback
function when the event occurs
● Click Event Listener: listens for a click event on a button element
and executes a function when clicked.
button.addEventListener('click', function() {
console.log('Button clicked!');
});
● Double Click Event Listener: This listens for a double click event on
a button element and executes a function when double clicked.

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.

It's an alternative to using multiple if statements for such scenarios.

let day = "Monday";

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.");
}

18. define flow control


● Flow control directs code execution based on conditions.
● It's like giving instructions on what to do next depending on the
situation.
● We can make decisions, repeat actions, or execute specific code
based on the program.
● This helps to create dynamic and flexible scripts that can respond
to various scenarios.
● This includes conditional statements, looping, and switch
statements.

19. if-else statement


● If: Executes a block of code if a specified condition is true.

let temperature = 25;


if (temperature > 20) {
console.log("It's a warm day!"); // Output: It's a warm day!
}

● Else: Executes a block of code if the ‘if’ condition is false.


let age = 20;
if (age >= 18) {
console.log("You are an adult."); // Output
} else {
console.log("You are a minor.");
}

● Else if: Allows you to check additional conditions if the previous


condition is false.

let score = 75;


if (score >= 90) {
console.log("You got an A!");
} else if (score >= 80) {
console.log("You got a B!");
} else {
console.log("You need to study harder!");
}

20. template literals ( eg. )


● Template literals provide a way to create strings that can include
variables or expressions by using $ symbol and {} curly brackets.
● They are enclosed by backticks (`) instead of single or double
quotes.
let message =
`Hello, my name is ${name}.My age is ${age}.`;

21. how do we use backtick ( ` ) strings, how are


they different from normal strings
● To embed expressions or variables within the string, you use ${ }
syntax. Inside ${ }, you can place any valid expression or variable
name.
● This syntax will be enclosed by backticks with additional
messages or information.

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.

22. diff.three types of defining in string ( ', ", ` )


● Single Quotes ('):
➢ Encloses string using single quotes.
➢ Can be used to create simple strings without any special
characters
let message = 'Hello, World!';

● Double Quotes ("):


➢ Encloses string literals using double quotes.
➢ Similar to single quotes, can be used for simple strings
without any special characters.
let message = "Hello, World!";
● Backticks (`):
➢ Used for template literals.
➢ Allow embedding expressions and variables within the string
using ${} syntax.
let message =
`Hello, my name is ${name}.My age is ${age}.`;

You might also like