Fundamentals of JavaScript_document
Fundamentals of JavaScript_document
Answer:
JavaScript is a high-level, interpreted programming language primarily used for creating interactive
effects within web browsers. It is often used alongside HTML and CSS to build dynamic web
pages and web applications.
Answer:
JavaScript has primitive data types like string, number, boolean, null, undefined, and symbol
(added in ES6), along with the complex data type object.
Answer:
== is the equality operator, which checks for equality after type coercion, while === is the strict
equality operator, which checks for equality without type coercion.
4. What are functions in JavaScript?Answer: How do you define and call them?
Answer:
Functions in JavaScript are blocks of code that perform a specific task. They are defined using the
function keyword, followed by the function name and parameters (if any).
console.log(sayHello(‘AjayReddy’));
Answer:
In JavaScript, single-line comments start with //, and multi-line comments are enclosed between /*
and */.
Answer:
var is function-scoped, let and const are block-scoped. Variables declared with const cannot be
reassigned, while variables declared with let can be reassigned. Additionally, const requires
initialization during declaration.
Answer:
console.log () is used to print messages to the console for debugging purposes. It helps developers
inspect values, track the flow of execution, and identify errors in their code.
8. What are the different ways to create objects in JavaScript?
Answer:
Objects can be created using object literals, the new keyword, constructor functions, or with
Object.create() method.
Answer:
this refers to the object to which a method belongs or the context in which a function is called. Its
value is determined at runtime.
Answer:
null represents the intentional absence of any object value, while undefined represents the absence
of a value or uninitialized variable.
Answer:
Arrow functions are a short and concise way of writing functions in JavaScript. The general syntax
of an arrow function is as below:
console.log("hello world!");
};
Answer:
Hoisting in javascript is the default process behavior of moving declaration of all the variables and
functions on top of the scope where scope can be either local or global.
function hoistedFunction( ){
Answer:
NaN property in JavaScript is the “Not-a-Number” value that is not a legal number.
14. Define REST parameter in JavaScript.
Answer:
Rest parameter is used to declare the function with improved handling of parameters. Rest
parameter syntax can be used to create functions to perform functions on the variable number of
arguments. It also helps to convert any number of arguments into an array as well as helps in
extracting some or all parts of the arguments.
Answer:
In a function call, we use the spread operator. It's also to spread one or more arguments that are
expected in a function call.The spread operator is used to take an array or an object and spread
them.
Answer:
Object destructuring is a method to extract elements from an array or an object.const arr = [1, 2,
3];
console.log(first); // Outputs 1
console.log(second); // Outputs 2
console.log(third); // Outputs 3
Answer:
Async-await functions are executed sequentially one after another in an easier way.
Answer:
DOM stands for Document Object Model. DOM is a programming interface for HTML and XML
documents.When the browser tries to render an HTML document, it creates an object based on the
HTML document called DOM. Using this DOM, we can manipulate or change various elements
inside the HTML document.
Answer:
document is an object and writeln is a method used for to print content on browser.
Answer:
we can define arrays using the array literal as follows: var arr=[10,20,30];
21. List out the different ways an HTML element can be accessed in a JavaScript code.
Answer:
Here, these are the the list of ways an HTML element can be accessed in a Javascript code:
(i) getElementById(‘idname’): Gets an element by its ID name
(ii) getElementsByClass(‘classname’): Gets all the elements that have the given
classname.
(iii) getElementsByTagName(‘tagname’): Gets all the elements that have the given tag
name.
(iv) querySelector(): This function takes css style selector and returns the first selected
element.
22. In how many ways a JavaScript code can be involved in an HTML file?
Answer:
There are 3 different ways in which a JavaScript code can be involved in an HTML file:
a. Inline
b. Internal
c. External
23. What is Anonymous function in JavaScript?
Answer:
These types of functions doesn't contain any name. They are declared dynamically at runtime.
var display=function( ) {
document.writeln("Anonymous Function");
display( );
Answer:
In JavaScript, we need closures when a variable which is defined outside the scope in reference is
accessed from some inner scope.
function sum( )
document.writeln(num+num);
sum( );
a. By object literal
b. By creating an instance of Object
c. By Object Constructor
Answer:
a. By array literal
b. By creating an instance of Array
c. By using an Array constructor
Answer:
charAt(0)
Answer:
Answer:
Variables that are not inside any function or the curly braces is known as Global Scope. These
variables can be accessed from all parts of the code.
Answer:
Variables declared inside a scope or function is known as function. Variables where it can be
accessed only within that function. That means they cannot be accessed outside code.
Answer:
Synchronous programming means that code is executed sequentially, one statement at a time.
Asynchronous programming, on the other hand, allows multiple operations to be performed
concurrently.
Answer:
Minimize DOM manipulation: Reduce the number of DOM queries and updates.
Use efficient data structures and algorithms.
Avoid unnecessary calculations and function calls.
Optimize loops by reducing the number of iterations and avoiding unnecessary work inside loops.
Minimize network requests and utilize caching where possible.
Use tools like browser developer tools and performance profiling to identify bottlenecks and optimize
critical code paths.
Answer:
Closures are functions that retain access to variables from their lexical scope even after the scope has
exited. This means that closure can access variables defined in its outer function even after that function
has finished executing.
Closures are created whenever a function is defined within another function. They are powerful because
they allow for data encapsulation and the creation of private variables in JavaScript.
Answer:
Hoisting is the default behaviour of javascript where all the variable and function declarations are moved
on top.This means that irrespective of where the variables and functions are declared, they are moved on
top of the scope. The scope can be both local and global.
hoistedFunction();
function hoistedFunction(){
Answer:
The debugger for the browser must be activated in order to debug the code. Built-in debuggers may be
switched on and off, requiring the user to report faults. The remaining section of the code should stop
execution before moving on to the next line while debugging.
Answer:
‘Hello World’
Answer:
The variable typing is the type of variable used to store a number and using that same variable to assign
a “string”.
Name=40;
Name=”Edunet foundation”
Answer:
Alert
Confirm
Prompt
39. What is the difference between an alert box and a confirmation box?
Answer:
An alert box will display only one button which is the OK button. It is used to inform the user about the
agreement has to agree. But a Confirmation box displays two buttons OK and cancel, where the user can
decide to agree or not.
Answer:
There are lots of disadvantages of using the innerHTML in JavaScript as the content will replace
everywhere. If you use += like “innerHTML = innerHTML + ‘html'” still the old content is replaced by
HTML. It preserves event handlers attached to any DOM elements.
Answer:
The try statement lets you test a block of code to check for errors.
The catch statement lets you handle the error if any are present.
The throw statement lets you make your errors.
Answer:
It is used to remove focus from the selected element. This method starts the blur event or it can be
attached to a function to run when a blur event occurs.
Answer:
document.getElementById("GFG").checked;
Answer:
Syntax error: A syntax error is an error in the syntax of a sequence of characters or tokens that are
intended to be written in a particular programming language.
Logical error: It is the most difficult error to trace as it is the error on the logical part of the coding or
logical error is a bug in a program that causes to operate incorrectly and terminate abnormally.
Runtime Error: A runtime error is an error that occurs during the running of the program, also known as
an exception.
Answer:
JavaScript is the main language that has to maintain some rules and regulations which is ECMA Script,
these rules also bring new features for the language JavaScript.
Answer:
By pressing the F12 we can trigger the debugging mode of any browser and can view the result by taping
the console.
Answer:
(function () {
return // body of the function
}());
Answer:
Here, 3 and 2 behave like an integer, and “7” behaves like a string. So 3 plus 2 will be 5. Then the output
will be 5+”7? = 57.
49. Which company developed JavaScript?
Answer:
Netscape developed JavaScript and was created by Brenden Eich in the year of 1995.
Answer:
Answer:
SessionState: It is user specific that can access all the data on the web pages.
Answer:
Answer:
Answer:
In JavaScript, parseInt() function is used to convert the string to an integer. This function returns an
integer of base which is specified in second argument of parseInt() function. The parseInt() function
returns Nan (not a number) when the string doesn’t contain number.
Answer:
document.getElementById("myText").style.fontSize = "16px;
Answer:
In JavaScript, a cookie is a small piece of data stored on the client-side (user's browser) that persists
across sessions. Cookies are commonly used to store information such as user preferences, shopping cart
items, or session identifiers.
Answer:
We can create a cookie in JavaScript using the document.cookie property. Example given below
document.cookie=” username=Ajay; expires=Thu, 01 Apr 2024 00:00:00 UTC; “
Answer:
To delete a cookie in JavaScript, you can set its expiration date to a time in the past. Like below
Answer:
No, cookies are not inherently secure for storing sensitive information because they can be accessed and
modified by the client-side code or intercepted in transit. For sensitive information, it's recommended to
use other mechanisms like HTTPS and server-side storage with proper encryption.
Answer:
Answer:
No
Answer:
012
Answer:
Yes
Answer:
Answer:
66. What is the result of trying to extend the length of an array using a function in JavaScript?
function extendArray(arr) {
arr.push(5);
}
let myArr = [1, 2, 3];
extendArray(myArr);
console.log(myArr.length);
Answer:
Answer:
[1,2,3,4]
68. What will be the output of this code snippet?
let numbers = [1, 2, 3];
numbers[10] = 11;
console.log(numbers.length);
Answer:
11
Answer:
Answer:
Object
Answer:
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
Answer:
Answer:
Answer:
try {
let x = y;
} catch(e) {
console.log(typeof e);
}
Answer:
Object
Answer:
Answer: 1
Answer:
Answer:
Answer:
The process of breaking down a function into a series of functions that each take a single argument.
Answer:
Answer:
50
class Animal {
speak() {
return 'I am an animal';
}
}
class Dog extends Animal {
speak() {
return super.speak() + ', but more specifically a dog';
}
}
let dog = new Dog();
console.log(dog.speak());
Answer:
Answer:
85. What will be the result of this code if localStorage already has 10 items?
Answer:
15
Answer:
The result will be True for 1st and 2nd case and False for the 3rd case.
87. Write a function that takes a string as input and returns the string reversed.
function reverseString(str){
return str.split('').reverse().join('');}
console.log(reverseString('hello')); //
Answer: 'olleh'
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
console.log(factorial(5)); //
Answer:
89. Write a function to check if a given string is a palindrome (reads the same forwards and
backwards).
function isPalindrome(str) {
console.log(isPalindrome('radar'));
console.log(isPalindrome('hello'));
Answer:
True
False
90. Find the Longest Word: Write a function that takes a sentence as input an returns the longest
word in it.
function longestWord(sentence) {
Answer:
'jumps'