HTML
Answer in one word or one sentence
1. What is JavaScript syntax?
Ans) JavaScript syntax refers to a set of rules that determines how the language to be written and
interpreted.
2. What is a variable?
Ans) A variable is the name given to memory location, to hold
a value
3. What is an identifier?
Ans) An identifier is a name given to an entity, which distinctly
identifies an entity at the time of execution
4. What is a statement?
Ans) Set of instructions to be executed by the computer are called statements.
5. What is a code block?
Ans) The statements to be executed together are defined inside a curly brackets {…} these are called
as code blocks.
6. What is inline JavaScript?
In Inline JavaScript the code is written where it needs to be called.
7. What is the syntax for embedded JavaScript?
//Embedding script
<head>
<script type=""text/javascript>
function function_name(){
.......
</script>
</head>
8. What is the syntax for external JavaScript?
Define the script in a separate file and link them using src attribute of script tag.
<script type="text/javascript" src="[Link]">
</script>
[Link]
function myfunc(){....}
9. What is an Alert box in JavaScript?
An alert box is used often if you want to make sure information is passed to user. An alert box pops
up and user have to click "OK" to proceed.
Eg; [Link]("Proceed");
10. What is the use of confirmation box in JavaScript?
Confirm() method displays a dialog box with a specific message along with an OK button and CANCEL
button.
-It is often used when user want to verify or accept something.
11. What is the use of a break statement in JavaScript?
Ans) Hyper Text Markup Language Break statements are often used to stop the flow of execution in a
loop. If we are not using the break statement the loop will execute forever.
12. Write a code to execute an infinite loop in JavaScript.
Ans) i=1
While(i<=10)
[Link](x)
13. What is nested loop?
Ans) Composition of multiple loops are called nested loops. That is, one loop inside another loop.
The first loop is called outer loop and next loop is called inner loop. Outer loop executes first, and
inner loop executes each time outer loop executes once.
14. What is recursion?
Ans) Recursion is a way of programming where a function calls itself one or more times in its body.
Usually it is returning the return value of this function call.
15. Output of a test expression given inside an if statement will be:
Ans) The program evaluates the test expression and will execute the statement only if the test
expression is True. So the output will be the true part under if statement.
Answer the following
1. How can you initialiase a variable in JavaScript? Explain with examples.
Variables in JavaScript can be initialised using the
keyword 'var' . var_x, var_y, a, b are variables.
Eg: Var var_x,var_y;
var_x = 54;
var_y=34;
2. What are identifiers and how are they different from variables? Provide examples.
Ans) An identifier is a name given to an entity, which distinctly identifies an entity at the time of
execution. Variable is also known as an identifier, its name uniquely identifies itself in the program.
ie, identifier is a name given to the entity, where as variable is the name given to memory location, to
hold a value.
3. What are JavaScript statements? Explain with examples.
Ans) A program consists of set of instructions to be executed by the computer. These instructions are
called statements. A JavaScript program is a list of statements. It is a piece of code that the computer
can execute.
A statement is composed of: values, operands, operators, expression, keywords and comments.
Eg; var x, y, z //statement 1
x = 5; //statement 2
y=7; // statement3
z = x+y; //statement 4
4. How can you comment in a code in JavaScript? Explain with examples.
Ans) Codes in JavaScript can be commented by putting the between double slashes // or between
/*...*/.// is single line comment and /*....*/ is multiline comment. Comments are ignored while
executing.
It can also be used as a code explanation.
Eg:
var x,y; //This is executed
//var x=8; //This is not executed.
5. What is a JavaScript keyword? Explain with examples.
Ans) JavaScript keywords are used to identify the actions to be performed.
Key word is actually a reserved word with a special meaning.
They cannot is used as a variable.
Example,
var is a keyword that tells the browser to create variables.
var var_x;
var a,b;
if, else, for, break..etc are also some another keywords
6. What are the different ways of including a JavaScript code in HTML? Explain.
Inline:
Inline JavaScript the code is written where it needs to be called.
<button type="button" onclick="[Link]('demo').innerHTML='Paragraph
Changed' ">Click Me!</button>
Embedded:
When JavaScript code is written between the <script> and </script> tags in the same HTML file. This
is known as embedded JavaScript.
<head>
<script type=""text/javascript>
function function_name(){
.......
</script>
</head>
External:
Define the script in a separate file and link them using src attribute of script [Link] is called external
JavaScript.
<script type="text/javascript" src="[Link]">
</script>
[Link]
function myfunc(){....}
7. Explain the different options of using a prompt box.
A prompt box is used when the user wants to input a value. When the prompt box pop up, the user
will have to click "OK" or "Cancel" to proceed after entering an input value.
- If the user clicks "OK" the box returns the input value.
- If the user clicks "cancel" null is returned.
The different options of using prompt are:
1. prompt("what is your name");
2. prompt("what is our name", "please enter here");
3. [Link]("what is your name","pease enter
here")
8. Write an example for inline JavaScript and change the same code to embedded JavaScript.
Inline:
<button type="button" onclick=
"alert('Hello cyberkid');">Click me
<button>
Embedded:
<script>
function clickme(){
[Link]('btn');}
</script>
<button onclick="clickme" id="btn">
Click here</button>
9. Write an example for embedded JavaScript and change the same code to external
JavaScript.
Embedded script:
<script>
function clickme(){
[Link]('btn'); }
</script>
<button onclick="clickme" id="btn">
Click here</button>
External script:
<script type="text/javascript" src="[Link]">
</script>
[Link]
function clickme(){
[Link]('btn');}
10. Write down the syntax of JavaScript if….else if ladder with an example.
Ans)Syntax:
If(condition1)
{ //statements;
}else if (condition2){
//statements;
}else if(condition3){
//statements;
}else{
//statements;
Example: Program to check whether the given number is positive ,negative or equal to zero.
Var x=prompt(“Enter a number”);
if(x>0){
[Link]("positive number");
else if(x<0){
[Link]("negative number");
else if(x=0){
[Link]("zero");
else{
[Link]("not defined");
2. What the difference is between nested if and if….else if ladder?
Ans) Nested if - In this case multiple if statements are executed one inside the other. It means, one if
statement is inside another if statement.
Eg; if(Condition A){
//executes when condition A is true
if (condition B){
//executes when condition B is true
If else if ladder- The else if statements allow to check multiple conditions and execute a particular
block of code. It can have multiple else if blocks
Eg:
If(condition1)
{ //statements;
}else if (condition2){
//statements;
}else if(condition3){
//statements;
}else{
//statements;
3. Write a program to input a number, find out whether it is greater than or less than 100.
Ans<!Doctype html>
<html>
<head>
</head>
<body>
<p> hello</p>
<script>
var x=prompt("Enter a number");
if (x>100)
[Link]("Number is greater than 100");
else
{
[Link]("Number is less than 100");
</Script>
</body>
</html>
[Link] is the use of loop in programming? What are the loops in JavaScript?
Ans)Loop is a piece of code which allows you to repeat a block of code more than once without
repeating the code.
These are the loops in javascript:
1. While loop [Link] loop 3. Recursion
1. A while loop statement repeatedly executes a
target statement as long as given condition is true.
[Link] loop also executes as long as the condition is
true and also it takes the number of iteration.
[Link] is a way of coding where a function calls itself one or more times in its body
5. What are loop control statements in JavaScript?
Ans) Loop control statements are used to handle the iteration of loops.
The most popular loop control statements are continue, break
-Break - These are used to stop the loop immediately.
The loop will stop execution.
Eg; if(password==guess){
[Link]("Success'');
break; }
else{
[Link]('failed');
-Continue - The continue statement skips the rest of statements in
the loop in current iteration and returns control to the beginning of the while loop.
We use this to restart the while, for loop.
While(i>10){
if(i%2 == 0){
continue;
alert("This is never executed");
else
{[Link](i); }