1. What is JavaScript?
Answer: JavaScript is a general purpose clientside object oriented programming language. Through JavaScript you can make your programme dynamic and interactive by adding various logics and behaviours in your web page. It is a case sensitive, event based, and dynamic type language. Today it has more than 50 popular frameworks for various purposes such as graph visualiation, 3d content development, ui development, animation etc.
2. How is JavaScript different from Java?
Answer: JavaScript runs on browsers, Java run on applet. In JavaScript browsers interprets then display the result on the browsers. In Java compiler compiles the code then display on applet viewer. In Java programmers use “javac” compiler. Java and JavaScript both need an environment to run on a machine. For Java you need to install JRE or Java Runtime Environment explicitly while for JavaScript you don’t need anything except a web browser as browsers offer inbuilt support for JavaScript.
3. How to embed JavaScript in a web page?
Answer: We can embed the JavaScript very easily in any of the page using the following approach.
Example:
<script language="JavaScript" type="text/JavaScript" > //Program goes here </script>
4. What are JavaScript Data Types?
Answer: When we create a program then we store some values to use in the application. What sort or type of value we are storing our program must know and this is what we call datatypes.
Although there are so many datatypes to discuss but I will write about only primitive datatypes:
- String
- Boolean
- Number
- Null
- Undefined
5. How to add JavaScript code to a button? Please write the complete code.
Answer: If we want to add any JavaScript code to a button means we are calling a block of code on particular event such as click. So first we must have a button and then we create a function to associate with button.
<html> <head> <title>Button Creation</title> </head> <body> <input type="button" id="first_btn" value="Submit"/> </body> <script> //declaring a function with an alert function myFunc(){ alert('I am working'); } //adding an event listener on onclick event with the button document.getElementById('first_btn').onclick = myFunc; </script> </html>
6. What boolean operators JavaScript support ?
Answer: &&(and), ||(or), and !(not). These operators JavaScript supports. && operator both or all conditions should be true.
Example:
// && when both or all conditions must be true if( a == 5 && b == 6 ) // || operator only one condition should be true if( a == 5 || b == 6 ) // ! operator means 'not' so it can be used when you are getting a boolean false value or != (not equal) if( a != 5 )
7. What is the scope in JavaScript ? Explain with Example.
Answer: General meaning of scope is accessibility. Normally we declare functions and variables in two ways i.e. Local and Global.
Local scope: you declare a function of variable inside a function so you can access or call that variable or function only inside that function.
Global scope: you can access a variable or function anywhere on the page. It means you you have declared them out of the function.
Example:
<script> //Note: please check both of the example one by one //EXAMPLE OF GLOBAL //this variable is global so we can access them in both of the following given functions. var firstNumber = 10, secondNumber = 10; function func1() { alert(firstNumber + secondNumber); //20 } function func2() { alert(firstNumber + secondNumber); //20 } //EXAMPLE OF LOCAL function func1() { //this variable is local so we cann't access them in both of the following given functions. var firstNumber = 10, secondNumber = 10; alert(firstNumber + secondNumber); // answer will be 20 } // this function can't access firstNumber and secondNumber variables as they are in a function function func2() { alert(firstNumber + secondNumber); //undefined variables } </script>
8. What does 1+’2’+4 evaluate to?
Answer: Answer of the above expression would be 124. Because ‘2’ is string type value so when we will add anything with it that will be concatenated and converts into a string.
9. What is the difference between alert() and confirm() popup boxes?
Answer: alert() displays value in popup box in browser and it has only one button i.e. OK. confirm() takes confirmation from users so we can use it in knowing the opinion of visitors. It has two buttons i.e. Yes and No.
In essence alert() popup we use to display the messages to the users or visitors while confirm() popup is good for taking choices of the visitors of our websites.
10. What types of comments in JavaScript are available?
Answer: There are two types of comments in JavaScript.
a) For Single line Comment we use // (double forward slash)
b) For Multiline comment we use /* */
/* // // */
11. What are the arguments or parameters in JavaScript?
Answer: To make our function more reusable and flexible we use parameters in JavaScript. In this way we can create a function more useful as it gives you an option to get different-2 results using one function. We use ‘return’ statement to achieve this in functions in JavaScript.
Note: Please see the question number 12.
12. What is use of return statement in JavaScript?
Answer: To return value from a function we use return statement in JavaScript. Let me elaborate it suppose you want to receive multiple results from a function in an application and all the results are being shown on different – 2 locations then you need to use return inside that function. Please see the following given example:
function helloFunc(name, email) { return 'hello '+name+!\n Your email id is '+email; } alert(helloFunc('Ravi Bhadauria', 'info@admecindia.co.in')); document.getElementById('divId').innerHTML = function (){ helloFunc('admec multimedia', 'admission@admecindia.co.in'); }
You can see how I have called helloFunc() in alert and in a div with id ‘divId’.
13. What if you get undefined value? What it means please explain.
Answer: That means either you haven’t stored or assigned any value in a variable or you haven’t declared a variable that you are accessing.
Example:
var num; alert(num); //undefined alert(newNum); //undefined
14. Write the differences between null and undefined.
Answer: Null is a value and that means a null value you have assigned in a variable. Undefined means you haven’t put any value in a variable which is being used in program. When you use an undefined value in some arithmetic operations such as (undefined + 5) then you get NaN in contrast (null + 5) is equal to 5 because null converts to false and that is 0.
15. Explain undefined and undeclared variables.
Answer: Undefined means you have declared a variable but not assigned a value in that. Undeclared means you haven’t declared a variable yet.
Example:
var a; alert(a); //undefined alert(b); //accessing an undeclared variable
16. Difference between == and === operators.
Answer: Both above operators fall in comparison operator category. This operator (==) is known as equality operator while === is strict equality operator.Equality operator only checks for the value while strict equality operator checks for the values and their types too.
Example:
var num1 = 5; var num2 = '5'; //it is a number but type of it is string as I have wrapped it in quotes if(num1 == num2); // will return true if(num1 === num2); // will return false
17. How to convert a string to a number in JavaScript? Write down all the important steps.
Answer: Conversion of a value from one type to other type is called datatype conversion or typecasting.There might be so many chances when you will get numeric values as string type. So when you add them then you don’t get expected results because they get concatenated. So we convert those values into numbers using Number(), parseInt(), and parseFloat() datatypes.
Example:
var num1 = 5; var num2 = '5'; //it is a number but type of it is string as I have wrapped it in quotes alert(num1+num2);// result would be 55 not 10 alert(num1 + Number(num2)); //result would be 10 as we have converted that string value to number using Number function