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

Basic_js

This document contains a comprehensive list of important JavaScript interview questions and answers, covering fundamental concepts such as data types, built-in methods, object and array creation, debugging techniques, and error handling. It also discusses the differences between BOM and DOM, as well as practical coding challenges related to JavaScript. The content serves as a valuable resource for preparing for JavaScript interviews.
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)
3 views

Basic_js

This document contains a comprehensive list of important JavaScript interview questions and answers, covering fundamental concepts such as data types, built-in methods, object and array creation, debugging techniques, and error handling. It also discusses the differences between BOM and DOM, as well as practical coding challenges related to JavaScript. The content serves as a valuable resource for preparing for JavaScript interviews.
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/ 8

Important Basic Javascript Interview questions:

1) what is Javascript?
Ans: JavaScript (js) is a lightweight object-oriented scripting language that is used by several websites for
scripting webpages. It is an interpreted, full-fledged programming language that enables dynamic
interactivity on websites when applied to an HTML document.
2) How many data types are used in Javascript?
Ans: Boolean - For true and false values
Null - For empty or unknown values
Undefined - For variables that are only declared and not defined or initialized
Number - For integer and floating-point numbers
String - For characters and alphanumeric values
Symbols - For unique identifiers for objects
Compound types:
Object - For collections or complex values
Array- Array is a collection of similar type of elements
3) What are some build in methods in javascript?
Ans: 1) Date() : Returns the present date and time
2) concat(): Joins two strings and returns the new string
3) push(): Adds an item to an array
4) pop(): Removes and also returns the last element of an array
5) round(): Rounds of the value to the nearest integer and then returns it
6) length(): Returns the length of a string

4) How do you create an object in JavaScript?


Ans: const student = {
name: 'John',
age: 17
}
5) How do you create an array in JavaScript?
Ans: var a = [];
var b = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

6) What are the most commonly used technique of code debugging?


Ans:
1. Console.log() Statements:
Use console.log() statements to print values and messages to the console. This helps you understand the
flow of your code and the values of variables at different points.\

EXAMPLE :
console.log('Debugging message');
console.log(variableName);

2. Debugger Statement:
Insert the debugger; statement in your code where you want to break and start debugging. When the
browser encounters
this statement, it will pause execution, allowing you to inspect variables and step through the code.
EXAMPLE :
function myFunction() {
debugger;
// Your code here
}
7) What are the different ways an HTML element can be accessed in a JavaScript code?
Ans:
Here are the ways an HTML element can be accessed in a JavaScript code:
1) getElementByClass(‘classname’): Gets all the HTML elements that have the specified classname.
2) getElementById(‘idname’): Gets an HTML element by its ID name.
3) getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified tagname.
4) querySelector(): Takes CSS style selector and returns the first selected HTML element.

8) What is the difference between Undefined and Undeclared and null in JavaScript?
Ans:
1) Undefined : Undefined means a variable has been declared but a value has not yet been assigned to
that variable.
2) Undeclared : Variables that are not declared or that do not exist in a program or application.
3) Null : Null is an assignment value that we can assign to any variable that is meant to contain no value.

9) Implicit Type Coercion in javascript (in detail with examples)?


Ans:
When the value of one data type is automatically converted into another data type, it is called Implicit
type coercion in javascript.
Example: String coercion
var x = 4;
var y = "4";
x + y // Returns "44"
Example: Boolean coercion
var a = 0;
var b = 32;
if(a) { console.log(a) } // This code will run inside the block as the value of x is 0(Falsy)
if(b) { console.log(b) } // This code will run inside the block as the value of y is 32 (Truthy)

10) NaN property in JavaScript


Ans: NaN property in JavaScript is the “Not-a-Number” value that is not a legal number.

11) Characteristics of javascript strict-mode?


1) Strict mode does not allow duplicate arguments and global variables.
2) One cannot use JavaScript keywords as a parameter or function name in strict mode.
3) Strict mode can be defined at the start of the script with the help of the keyword ‘use strict’.

12) How many type of errors in javscript?


Ans: There are three type of errors in javscript
1) Runtime error:Occur during the execution of the code and are not detected until the code is running
var result = divideByZero(); // Runtime error if divideByZero function is not defined
2) Logical error:These errors don't cause the program to crash or produce error messages, but they lead
to unexpected behavior or incorrect results
3) Syntax error: Occur when there is a mistake in the syntax of the code. This prevents the code from
being parsed and executed.
if (x > 5 // Missing closing parenthesis
console.log('x is greater than 5');
13) How to handle error in javascript?
Ans:
Use try and catch blocks we can handle javascript exceptions.To catch and handle exceptions. The code
inside the try block is executed, and if an exception occurs, the control is transferred to the corresponding
catch block
try {
// Code that may throw an exception
throw new Error('This is an example error');
} catch (error) {
// Handle the exception
console.error('Caught an error:', error.message);
} finally {
// Optional: Code that will be executed regardless of whether an exception occurred
console.log('This code always runs');
}
14) What are the pop-up boxes available in JavaScript?
Ans:
1. Alert Box: The alert() function is used to display a simple message to the user in a pop-up box. It
contains only an OK button, and the user can only dismiss the alert by clicking OK.
alert("This is an alert box!");
2. Confirm Box: The confirm() function is used to display a pop-up box with a message and two buttons:
OK and Cancel. It returns true if the user clicks OK and false if the user clicks Cancel.
var result = confirm("Do you want to proceed?");
if (result) {
// User clicked OK
console.log("User wants to proceed.");
} else {
// User clicked Cancel
console.log("User canceled the operation.");
}
3. Prompt Box: The prompt() function is used to display a pop-up box that prompts the user to enter
some input. It takes two parameters: the message to display and a default value for the input field. It
returns the value entered by the user or null if the user clicks Cancel.
var userInput = prompt("Please enter your name:", "John Doe");
if (userInput !== null) {
console.log("User entered: " + userInput);
} else {
console.log("User clicked Cancel.");
}

15) What is the difference between BOM and DOM objects?


Ans: BOM (Browser Object Model):

1. The BOM represents the interaction between the browser and the outside world, typically the user
and the operating system.
2. It includes objects such as window, which represents the browser window or tab, and navigator,
which provides information about the browser.
3. BOM is not standardized by W3C, and different browsers may have different implementations.
4. DOM (Document Object Model):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BOM Example</title>
</head>
<body>

<script>
// Accessing the window object
console.log(window.innerWidth); // width of the browser window

// Accessing the document object


console.log(document.title); // title of the document

// Accessing the navigator object


console.log(navigator.userAgent); // user agent string
</script>

</body>
</html>

1) window is used to get the width of the browser window.


2) document is used to get the title of the document.
3) navigator is used to get the user agent string, which provides information about the browser.

The DOM represents the structured document as a tree of objects, where each object corresponds to a part
of the document, such as elements, attributes, and text.
It is a programming interface for web documents. It allows scripts to dynamically access and manipulate
the content, structure, and style of a document.
DOM is standardized by the W3C, making it consistent across different browsers.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM Example</title>
</head>
<body>
<!-- A simple HTML structure -->
<div id="myDiv">
<p>Hello, <span id="mySpan">world</span>!</p>
</div>
<script>
// Accessing and manipulating the DOM
var myDiv = document.getElementById('myDiv'); // Accessing an element by ID
myDiv.style.backgroundColor = 'lightblue'; // Changing the background color
var mySpan = document.getElementById('mySpan');
mySpan.innerHTML = 'GPT-3'; // Changing the content of a span
// Creating a new element and appending it to the document
var newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
document.body.appendChild(newParagraph);
</script>
</body>
</html>

1) getElementById is used to access HTML elements by their ID.


2) Style properties are changed to modify the appearance of the myDiv element.
3) The content of the mySpan element is changed.
4) createElement and appendChild are used to dynamically create and append a new paragraph to the
document.

Javascript Practical Questions :


https://www.youtube.com/watch?v=6ANg6HvbehA&list=PLAx7-
E_inM6EkgZkrujZvewiM_QZRU4A2&index=4
1. Find duplicate elements from an array
2. Remove duplicate elements from an array
3. How To find max/min in a given array in Javascript|
4. what is the difference between '==' and '===' operator
5. How do you find the second-largest value in the array
6. What is the difference between find() and filter()
7. How to Find missing elements in a given Array 1 to 10
8. How to find even or odd numbers in an array in Javascript
9. How to find the sum of all elements in an array in Javascript
10. How to find the factorial of a given number in Javascript
11. How to find prime number in Javascript
12. How to find vowels from the string in JavaScript
13. How to reverse a string in Javascript
14. How to find palindrome in JavaScript |
15. How to swap two variables without using the third
16. How to merge two arrays in JavaScript
17. How to find the factor of a given integer in JavaScript
18. Simple calculator in JavaScript
19. How to compare two Arrays are Equal or Not in JavaScript|
20. How to find the intersection of two arrays in JavaScript
21. How to convert kilometers into miles in JavaScript
22. How to convert the first letter of a string into uppercase in JavaScript
23. How to find the Fibonacci sequence in JavaScript
24. How to Check the No of Occurrence of Character in String in JavaScript
25. How to print the table of any user-defined number
26. Program to Check Armstrong Number in JavaScript

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy