Javascript Interview Que

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

JavaScript, created by Brendan Eich in 1995, is one of the most widely used web development languages.

It was designed to
build dynamic web pages at first. A script is a JS program that may be added to the HTML of any web page. When the page
loads, these scripts execute automatically.

A language that was originally designed to build dynamic web pages may now be run on the server and on almost any device
that has the JavaScript Engine installed.

After HTML and CSS, JavaScript is the third biggest web technology. JavaScript is a scripting language that may be used to
construct online and mobile apps, web servers, games, and more. JavaScript is an object-oriented programming language that is
used to generate websites and applications. It was created with the intention of being used in a browser. Even today, the server-
side version of JavaScript known as Node.js may be used to create online and mobile apps, real-time applications, online
streaming applications, and videogames. Javascript frameworks, often known as inbuilt libraries, may be used to construct
desktop and mobile programs. Developers may save a lot of time on monotonous programming jobs by using these code
libraries, allowing them to focus on the production work of development.

1) What is JavaScript?

As mentioned above, Javascript is no longer just a lightweight language. It has become a powerful,
object-oriented, interpreted programming language that adds dynamic capabilities to HTML
pages. All the popular web browsers use it as a scripting language. It also is used in the backend to
create services in platforms like NodeJS.

2) How do you add JavaScript to a web page?


You can embed JavaScript directly into a web page using script tags, like the example below:

<head>

<title>Your Page Title</title>

<script language="JavaScript" type="text/javascript">

[Your JavaScript code here]

</script>

</head>

You can also link in to the HTML file, like in this example:

<head>

<title>Your Page Title</title>

<script type="text/javascript" src="YourJavaScriptFile.js"></script>

</head>

3) How do you add comments to JavaScript?


You can add either line comments or block comments to JavaScript.
// This is a line comment. It must stay on one line.

/* This is a

block comment. It can

span as many lines as you’d like.*/

4) Are there still local and global variables?


In modern JavaScript, the designations of global and local variables have become more exact. By definition, a global variable can be
accessed from anywhere within the program and have “global scope” or global visibility within the program. Local variables are
temporary variables that only have a scope within the function that generated them and have “local scope.”

// How to declare a local variable

In older versions of JavaScript, you declared a variable using the keyword “var”. In modern JavaScript, “var” has been replaced with
the keyword “let”. Because of this, the distinction between global and local variables becomes more sharply defined. You declare a
variable using “let”:

let myFirstName = “John”;

If a variable is not within a function, an if block, or a loop, the variable is global in scope.

5) What data types does JavaScript support?

The data types supported by JavaScript are:

 Undefined
 Null
 Boolean
 String
 Symbol
 Number
 Object

6) Is JavaScript a case-sensitive language?


Yes, JavaScript is a case-sensitive language. When you name a variable, you must use the exact case to access that variable again.
For that reason, like most case-sensitive languages, it is standard practice to use camelback notion where everything is in lowercase
except mid-word capitalization. For example:

let myFirstName = “Mary”;

8) What is the difference between Java and JavaScript?


Java is an object-oriented, compiled programming language designed to run in the Java Virtual Machine. JavaScript is an object-
oriented, interpreted scripting language designed to run in the browser or JavaScript engines like NodeJS.

9) What is the difference between null and undefined in JavaScript?


A variable is undefined when you declare it without an assigned value, like below:

var x;

Null actually has to be assigned to a variable:

var x = null;

12) How do you create an array in JavaScript?


You can create an array in JavaScript by using the object literal, like the example below:

var emptyArray = [];

var populatedArray = ['a', 'b', 'c', 'd', 'e'];

13. Difference between “ == “ and “ === “ operators.

Both are comparison operators. The difference between both the operators is that “==” is used to
compare values whereas, “ === “ is used to compare both values and types.

Example:

var x = 2;
var y = "2";
(x == y) // Returns true since the value of both x and y is the same
(x === y) // Returns false since the typeof x is "number" and typeof y is
"string"

14. Difference between var and let keyword in javascript.


Some differences are

1. From the very beginning, the 'var' keyword was used in JavaScript programming whereas the keyword 'let' was just
added in 2015.
2. The keyword 'Var' has a function scope. Anywhere in the function, the variable specified using var is accessible but in
‘let’ the scope of a variable declared with the 'let' keyword is limited to the block in which it is declared. Let's start with
a Block Scope.
3. In ECMAScript 2015, let and const are hoisted but not initialized. Referencing the variable in the block before the
variable declaration results in a ReferenceError because the variable is in a "temporal dead zone" from the start of the
block until the declaration is processed.
4. What would be the result of 3+2+”7″?
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.

5. What is the use of the isNaN function?


The number isNan function determines whether the passed value is NaN (Not a number) and is of the
type “Number”. In JavaScript, the value NaN is considered a type of number. It returns true if the
argument is not a number, else it returns false.

6. Which is faster in JavaScript and ASP script?


JavaScript is faster compared to ASP Script. JavaScript is a client-side scripting language and does not
depend on the server to execute. The ASP script is a server-side scripting language always dependable
on the server.

9. Which company developed JavaScript?

Netscape developed JavaScript and was created by Brenden Eich in the year of 1995.

8. Is it possible to break JavaScript Code into several lines?

Yes, it is possible to break the JavaScript code into several lines in a string statement. It can be broken by using the backslash ‘\’.

For example:

document.write("A Online Computer Science Portal\ for Geeks")

The code-breaking line is avoid by JavaScript which is not preferable.

let gfg= 10, GFG = 5,

Geeks =

gfg + GFG;
11. Write a JavaScript code for adding new elements dynamically.

<!DOCTYPE html>

<html lang="en">

<head>

<title>Document</title>

</head>

<body>

<button onclick="create()">

Click Here!

</button>

<script>

function create() {

let geeks = document.createElement('geeks');

geeks.textContent = "Geeksforgeeks";

geeks.setAttribute('class', 'note');

document.body.appendChild(geeks);

</script>

</body>

</html>

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