1. What is JavaScript?
JavaScript is a programming language used both on the client-side and server-side that allows you to make web pages interactive.
It improves the user experience of the web page by converting it from a static page into an interactive one (dynamic page).
2. What is the use of a JavaScript?
Some of the uses of JavaScript are:
- Adds interactive behavior to web pages
- Creates web and mobile apps
- Builds web servers and develops server applications
- Game development, etc.
3. What is the use of a document in JavaScript?
JavaScript Document object is an object that provides access to all HTML elements of a document. It has various properties that refer to other objects which allow us to access and modify the document content.
4. What are the differences between JavaScript and Java?
JavaScript | Java |
---|---|
JavaScript is a scripting language | Java is a programming language |
JavaScript code is run on a browser only | Java creates applications that run in a virtual machine or browser |
JavaScript file has the file extension .js | Java file has the file extension .java |
5. How to print in JS?
We can use the
Example:
6. What is execution context?
Execution Context
- The environment in which Code runs is called Execution Context.
- Execution context contains all the variables, objects, and functions.
- Execution Context is destroyed and recreated whenever we reload an Application.
7. How to display the output in JavaScript (not in the console)?
JavaScript can display data in different ways:
textContent
innerHTML
document.write()
alert
8. List some of the advantages of JavaScript.
- Server interaction is less.
- Feedback to the visitors is immediate.
- Interactivity is high.
9. What is the output of 10+20+"30" in JavaScript?
3030
10. What language is JavaScript?
JavaScript is known as the Scripting Language for Web pages.
It is used add interactivity in Webpages.
11. Where do you write JavaScript?
We can write JavaScript in the HTML
The HTML
12. How to add JS to HTML?
There are three ways to add JavaScript to HTML pages.
- Embedding code
- Inline code
- External file
Embedding code: We can use the HTML
Inline code: It can be used when we have to call a function in the HTML event attributes.
External file: We can also create a separate file to hold the code of JavaScript with the
13. What is the solution to the absence of an HTML script element without an HTML src attribute?
The solution to the absence of an HTML
Example
14. Who created JavaScript?
The JavaScript was created by Brendan Eich at Netscape.
15. How to convert a string to a number?
We can convert a string to a number using the
16. What is an alert method in JS?
The alert methods instructs the browser to display a dialog with an optional message, and to wait until the user dismisses the dialog.
Example:
17. How do you debug errors in JavaScript?
Using
console.log()MethodSetting Breakpoints or using Debugger in code
Using Browser Debugging tools
To know more about debugging errors, refer to below references.
18. How can we submit a form in js?
We can submit the form,
- By clicking the HTML buttonelement with type submit.
- By clicking the HTML inputelement with type submit.
19. How to implement the form validation in JavaScript?
You can refer to this session code for implementing form validation in JavaScript.
20. How many scopes of a variable does JavaScript have?
JavaScript Variable has two scopes, local and global.
Local Scope:
If a variable is declared with
Example:
Global Scope:
If a variable is declared outside all functions and curly braces (
Example:
21. What are functions hoisting and variables hoisting?
Hoisting is a JavaScript technique that moves variables and function declarations to the top of their scope before code execution begins.
Example:
Output:
Even though the function declaration is after the function call, the function is called. This is possible due to function hoisting.
22. What are the differences between class and object?
Class | object |
---|---|
Class is a template for creating objects in a program | Object is an instance of a class |
Class is a logical entity | Object is a physical entity |
Class does not allocate memory space | Object allocates memory space |
You can declare class only once | You can create more than one object using a class |
Classes can't be manipulated | objects can be manipulated |
Classes doesn't have any Values | Objects have its own values |
You can create a class using the "class" keyword | You can create an object using the "new" keyword in JavaScript |
23. Define class and object?
Class:
The Class is a special type of function used for creating multiple objects.
Syntax:
Object:
- An Object is a collection of properties.
- A property is an association between a name (or key) and a value.
- For example, a person has a name, age, city, etc. These are the properties of the person.
24. What is Class Inheritance in JavaScript?
Inheritance is a mechanism by which a class inherits methods and properties from another class.
Syntax:
Here,
The
extendskeyword is used to inherit the methods and properties of the superclass.Calling
super()makes sure that SuperClass constructor() gets called and initializes the instance.