JavaScript Interview Questions(2)

1. If we want to return the character from a specific index which method is used?

  • The JavaScript string charAt() method is used to find out a char value present at the specified index.
  • The index number starts from 0 and goes to n-1, where n is the length of the string.
  • The index value can’t be a negative, greater than or equal to the length of the string.
var str="Javatpoint";
document.writeln(str.charAt(4));

2. What is the difference between JavaScript and JScript?

  • Netscape provided the JavaScript language.
  • Microsoft changed the name and called it JScript to avoid the trademark issue.
  • In other words, you can say JScript is the same as JavaScript, but Microsoft provides it.

3. How to write a hello world example of JavaScript?

  • The script tag specifies that we are using JavaScript.

  • The text/javascript is the content type that provides information to the browser about the data.

  • The document.write() function is used to display dynamic content through JavaScript.

<script type="text/javascript">
document.write("JavaScript Hello World!");
</script>

4.What are the key differences between Java and JavaScript? / How is JavaScript different from Java?

  • JavaScript is a lightweight programming language (most commonly known as scripting language) developed by Netscape, Inc.
  • It is used to make web pages interactive. It is not a part of the Java platform.
Java JavaScript
Java is a complete and Strongly typed programming language used for backend coding.
In Java, variables must be declared first to use in the program, and the type of a variable is checked at compile-time.
JavaScript is a weakly typed, lightweight programming language (most commonly known as scripting language) and has more relaxed syntax and rules.
Java is an object-oriented programming (OOPS) language or structured programming languages such as C, C++, or .Net. JavaScript is a client-side scripting language, and it doesn’t fully support the OOPS concept. It resides inside the HTML documents and is used to make web pages interactive (not achievable with simple HTML).
Java creates applications that can run in any virtual machine (JVM) or browser. JavaScript code can run only in the browser, but it can now run on the server via Node.js.
The Java code needs to be compiled. The JavaScr ipt code doesn't require to be complied.
Java Objects are class-based. You can’t make any program in Java without creating a class. JavaScript Objects are prototype-based.
Java is a Complete and Standalone language that can be used in backend coding. JavaScript is assigned within a web page and integrates with its HTML content.
Java programs consume more memory. JavaScript code is used in HTML web pages and requires less memory. JavaScript code is used in HTML web pages and requires less memory.
The file extension of the Java program is written as “.Java” and it translates source code into bytecodes which are then executed by JVM (Java Virtual Machine). The JavaScript file extension is written as “.js” and it is interpreted but not compiled. Every browser has a JavaScript interpreter to execute the JS code.
Java supports multithr eading. JavaScript doesn't support multithreading.
Java uses a thread-based approach to concurrency. JavaScript uses an event-based approach to concurrency.

5. How to use external JavaScript file?

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

6.Is JavaScript case sensitive language?

Yes, JavaScript is a case sensitive language. For example:

Var msg = "JavaScript is a case-sensitive language"; //Here, var should be used to declare a variable
function display()
{<!-- -->
document.writeln(msg); // It will not display the result.
}
display();

7. What is BOM?

  • BOM stands for Browser Object Model.
  • It provides interaction with the browser.
  • The default object of a browser is a window.
  • So, you can call all the functions of the window by specifying the window or directly.
  • The window object provides various properties like document, history, screen, navigator, location, innerHeight, innerWidth

8. What is DOM? What is the use of document object?

  • DOM stands for Document Object Model.
  • A document object represents the HTML document.
  • It can be used to access and change the content of HTML.

Method Description
write(“string”) writes the given string on the doucment.
writeln(“string”) writes the given string on the doucment with newline character at the end.
getElementById() returns the element having the given id value.
getElementsByName() returns all the elements having the given name value.
getElementsByTagName() returns all the elements having the given tag name.
getElementsByClassName() returns all the elements having the given class name.
<script type="text/javascript">
function printvalue(){<!-- -->
var name=document. form1. name. value;
alert("Welcome: " + name);
}
</script>
  
<form name="form1">
Enter Name: <input type="text" name="name"/>
<input type="button" onclick="printvalue()" value="print name"/>
</form>

9. What is the use of window object?

  • The window object represents a window in browser.
  • An object of window is created automatically by the browser.
Method Description
alert() displays the alert box containing message with ok button.
confirm() displays the confirm dialog box containing message with ok and cancel button.
prompt() displays a dialog box to get input from the user.
open( ) opens the new window.
close() closes the current window.
setTimeout() performs action after specified time like calling function, evaluating expressions etc.

Example of alert() in javascript

<script type="text/javascript">
function msg(){<!-- -->
 alert("Hello Alert Box");
}
</script>
<input type="button" value="click" onclick="msg()"/>

Example of confirm() in javascript

<script type="text/javascript">
function msg(){<!-- -->
var v= confirm("Are u sure?");
if(v==true){<!-- -->
alert("ok");
}
else{<!-- -->
alert("cancel");
}
  
}
</script>
  
<input type="button" value="delete record" onclick="msg()"/>

Example of prompt() in javascript

<script type="text/javascript">
function msg(){<!-- -->
var v= prompt("Who are you?");
alert("I am " + v);
  
}
</script>
  
<input type="button" value="click" onclick="msg()"/>

Example of open() in javascript

<script type="text/javascript">
function msg(){<!-- -->
open("http://www.javatpoint.com");
}
</script>
<input type="button" value="javatpoint" onclick="msg()"/>

Example of setTimeout() in javascript

<script type="text/javascript">
function msg(){<!-- -->
setTimeout(
function(){<!-- -->
alert("Welcome to Javatpoint after 2 seconds")
},2000);
  
}
</script>
  
<input type="button" value="click" onclick="msg()"/>

10. What is the use of history object?

  • The history object of a browser can be used to switch to history pages such as back and forward from the current page or another page.
  • There are three methods of history object.
    • history.back() – It loads the previous page.
    • history.forward() – It loads the next page.
    • history.go(number) – The number may be positive for forward, negative for backward. It loads the given page number.
history.back();//for previous page
history.forward();//for next page
history.go(2);//for next 2nd page
history.go(-2);//for previous 2nd page