Thursday, September 10, 2015

JavaScript Questions and Answers for Beginners

Q-1: What is JavaScript?
Ans: JavaScript is a programming language using which we make apps, dynamic design & develop responsive website, games, etc. JavaScript is an interpreted language. JavaScript when applied to an HTML document, can provide dynamic interactivity on websites. Most importantly, JavaScript isn't overlay tough to use. Its syntax is quite similar to C, C++ and Java.

Q-2: How is JavaScript different from Java ?
Ans-:
Java:
Java is a statically typed language.
Java is class-based.
Java uses block-based scoping.
Java code needs to be compiled.
Java creates applications that run in a virtual machine or browser.

Javascript:
JavaScript is dynamic.
JavaScript is prototype-based.
JavaScript uses function-based scoping.
JavaScript code are all in text.
JavaScript code is run on a browser only.

Q-3: How to embed JavaScript in a web page ?
Ans-: JavaScript code can be embed in a web page in two ways.
    a. <script src="my-external-javascript-path.js" type="text/javascript"
</script>
   b. <script type="text/javascript"></script>

Q-4:What are JavaScript Datatype ?
Ans-: In JavaScript, there are data types.
    1.String
    2.Number
    3.Boolean
    4.Array
    5.Object
    6.Null
    7.Undefined

Q-5: How to add JavaScript to a button? please write the complete code.
Ans-:<button id="click"></button>

Q-6: What Boolean operators JavaScript supports ?

Ans-:  Boolean is a object & datatype.
     Boolean operators in JavaScript are as under
    &&, || and !

Q-7: What is scope in JavaScript ? Explain with appropriate examples.
Ans-: Scope refers to where variables and functions are accessible, and in what context it is being executed.There are two type of Scope.
1. Local
Example:
    <!DOCTYPE html>
    <html>
            <body>
            <script>
        var a = 99;
        function hello() {
            var x = 5;
            alert("Hello, " + (a + x) + "!");
        }
        hello(); // prints the string "Hello, 104!"
    </script>
    <body>
    </html>

2.Global
Example:
    <!DOCTYPE html>
    <html>
            <body>
            <script>
        var a = 99;
        function hello() {
            alert("Hello, " + a + "!");
        }
        hello(); // prints the string "Hello, 99!"
        alert(a); // prints the number 99
   </script>
    <body>
    </html>

Q8: What does 1+'2'+4 evaluate to ?
Ans-: '2' is string, everything is a string, so the result is 124.
   
Q-9: What is the difference between alert() and confirm() popup boxes ?
Ans-:
1. Alert Box: An alert box is often used when you want to make sure information comes through to the user. When an alert box popups, the user will have to click “Ok” to proceed.

2. Confirm Box: A confirm box is often used when you want the user to verify or accept something. When a confirm box pops up , the user will have to click either “Ok” or “Cancel” to proceed. If user clicks “Ok”, the box returns true. If user clicks “Cancel”, the box returns false.

Q-10: What type of comments in JavaScript are available ?

Ans-: There are two type of comments
    1. Multiple lines start with        /* */
    2. Single lines start with          //
   
Q-11: .What is the use of return statement in JavaScript ?
Ans-:  The return statement ends function execution and specifies a value to be returned to the function caller.

Calculate the product of two numbers, and return the result:

<!DOCTYPE html>
<html>
<body>
    <p id="example"></p>
<script>
var x = myFunction(25, 50);       // Function is called, return value will end up in x

function myFunction(a, b) {
    return a + b;         // Function returns the product of a and b
}   
document.getElementById("example").innerHTML = x;
</script>

</body>
</html>
        The result of x will be: 75

Q-12: Write down the differences between null and undefined  ?
Ans-:Undefined value cannot be explicitly stated that is there is no keyword called undefined whereas null value has keyword called null.

typeof undefined variable or property returns undefined whereas typeof null value returns object.

Q-13: Write down about undefined and undeclared variables ?
Ans-: Undefined variables are those that are not assigned any value but are declared in the program.Trying to read such variables gives special value called undefined value.

Undeclared variables are those that are not declared in the program (do not exist at all),trying to read their values gives runtime error.But if undeclared variables are assigned then implicit declaration is done .

Q-14: Explain variable typing or dynamic typing in JavaScript ?
Ans-: It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows
example
i = 10;
i = "string";
This is called variable typing

No comments:

Post a Comment

Featured Post

ADMEC Multimedia Institute Scholarship Program

The ADMEC Multimedia Institute scholarship program aims to select and trained talented non working female, married woman and non married m...