Advanced JavaScript: Notes
- Categories JavaScript, Advanced JavaScript
There are various scripting languages that can be used to create dynamic websites. JavaScript is one such interpreted scripting language as it executes its instructions directly and freely without compiling machine language instructions. This post contains notes in the form of questions and answers to help you better understand JavaScript.
- What a is platform independent software?
Ans.
Software that can run on any hardware platform (PC, Mac, SunSparc etc.) or software platform (Windows, Linux, Mac OS etc.) is called as platform independent software. JavaScript is a platform independent scripting language.
- Why is JavaScript known as the universal client side scripting language?
Ans.
- Any JavaScript enabled browser can understand and interpret JavaScript code.
- Due to different features, JavaScript is known as universal client side scripting language.
- Write any five features of JavaScript language.
Ans.
- JavaScript is a light weight scripting language because it does not support all features of object oriented programming languages.
- No need of special software is required to run JavaScript programs.
- JavaScript is an object oriented scripting language and it supports event based programming facility.
- It is a case sensitive language.
- JavaScript helps the browser to perform input validation without wasting the user’s time by the web server access.
- It can handle date and time very effectively.
- Most of the JavaScript control statements syntax is same as the syntax of control statements in other programming languages.
- JavaScript language provides the ability to create new functions within scripts.
- A function can be declared in JavaScript using “function” keyword.
- JavaScript is a platform independent scripting language.
- Any JavaScript-enabled browser can understand and interpreted JavaScript code.
- Due to different features, JavaScript is known as universal client side scripting language.
- What are different types of scripting?
Ans.
Server side scripting and Client side scripting.
- Client-side Scripting: the script resides on client computer (browser) and can run on the client.
- Server-side Scripting: the script resides on web server.
- What is Client-side scripting in JavaScript?
Ans.
Client-side Scripting:
- The script resides on client computer (browser) and that can run on the client.
- These types of scripts are placed inside an HTML document.
- What is Server side scripting in JavaScript?
Ans.
Server side scripting:
- The script resides on web server.
- To execute the script it must be activated by client, and then it is executed on web server.
- What is the Difference between server side scripting and client side scripting?
Ans.
Server Side Scripting | Client Side Scripting |
1. Server-side scripting is used at the backend, where the source code is not visible or is hidden at the client side (browser). | 1. Client-side scripting is used at the frontend which users can see from the browser. |
2. Server-side scripting is more secure. | 2. Client-side scripting is less secure. |
3. When a Server-Side script is processed, it communicates to the server. | 3. Client-side scripting does not need any server interaction. |
4. Server-side scripting is useful in customizing the web pages and implements the dynamic changes in the websites. | 4. Client- side scripts are generally used for validation purpose and effectively minimize the load to the server. |
5. PHP, ASP.net, Ruby, ColdFusion, Python, C# are examples of server side scripting languages. | 5. HTML5, JavaScript are the examples of Client-side scripting languages. |
6. Web Server Software is required to execute server-side script | 6. Client-side script requires web browser as an interface. |
- How does the Switch case structure work?
Ans.
- The switch statement tests the value of a given expression against a list of case values.
- When a match is found, a block of statement associated with that case is executed.
- Why should there not be duplicity between the Switch cases?
Ans.
- The switch statement tests the value of given expression against a list of case values
- When a match is found, a block of statement associated with that case is executed. Thus, there should not be duplicity between the cases.
- What is the syntax for Switch Case Statement?
Ans.
switch(expression)
{
case value1:
statement block 1;
break;
case value2:
statement block 2;
break;
………………………
case value n:
statement block n;
break;
default:
statement block;
}
- What is a Looping Statement?
Ans.
- These are used to execute statements repeatedly while creating programming logic.
- Iteration is the execution of one or a group of statements of a code for fixed number of times or till the condition is satisfied.
- The condition is a Boolean condition.
- For…loop and while…loop are examples of looping statements.
- What is “for… loop”?
Ans.
- This loop executes the statement in the statement block as long as the condition is true.
- When condition becomes false, control is transferred out of the loop and executes the remaining program.
- It combines initialization, condition and loop iteration in a single statement.
- Initialization is assigning initial value to the variable, which executes only once, and then the condition is checked.
Example:
for(i=1;i<=5;i++)
{
document.writeln(i);
}
In above example “i” is the running variable and “i++” is incremental iteration of for loop.
- How does “while… loop” work?
Ans.
- First is initialization.
- Check condition: Then the loop will be executed repeatedly as long as the condition is true.
- Iteration: If condition is always true, then loop would be executed infinitely, so after some execution condition becomes false.
- Example:
VAR a=1 while(a<=5)
{
document.writeln(a);
a=a+1;
}
- What is a break statement?
Ans.
- A break statement is used to jump out of the loop.
- It is used to make an early exit out of a loop.
- When control encounters the keyword ‘break’, it automatically passes to the next statement after the loop.
- What is a continue statement?
Ans.
The continue statement in JavaScript is used when it is necessary to skip statement block and take the control at the beginning for the next iteration.
- What is a JavaScript object? Explain with a real world example.
Ans.
- A JavaScript object is an entity having state and behaviour.
- An object can group data together with functions needed to manipulate it.
- Real world objects examples are table, board, television, bicycle, shop, bus, car, monitor etc.
- Example: car object. It has properties like Name, Model, Weight, Color etc. and methods like Start, Stop and Brake etc.
Object | Properties | Methods |
car | car.name=Ferrari | car.start() |
car.model=F430 | car.drive() | |
car.weight=1517kg | car.brake() | |
car.color=red | car.stop() |
- What is a Document Object Model?
Ans.
The way in which HTML document content is accessed and modified is called as document object model. It is a programming interface for HTML and XML documents.
- What is W3C (World Wide Web Consortium) Document Object Model?
Ans.
The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.
- What is the innerHTML property?
Ans.
- The innerHTML property is useful for getting html element and changing its content.
- It can be used to get or change any html element, including <html> and <body>.
- What is the use of the Window Object Method prompt()?
Ans. Window Object Method prompt() is used to display a dialog box to get input from the user.
- Why do we use Window Object method setTimeout()?
Ans. setTimeout() method is used to call a function or evaluate an expression after a specified number of milliseconds.
- When do we use “onchange” event handler in JavaScript?
Ans. The “onchange” event occurs when the user changes the content of an element or selects dropdown value.
E.g. for textbox, password, select box, textarea, etc.
- Which event handler occurs when document/page has been unloaded or closes?
Ans. onunload
- What is the use of the String Object method indexOf() ?
Ans. It returns the index of the first occurrence of specified character in a given string, or -1 if it never occurs, so with that index you can determine if the string contains the specified character.
- Which String object method is used for returning the index of the last occurrence of specified character in given string?
Ans. lastIndexOf()
- Which String object methods are used to convert string to lower/ upper case?
Ans.
- toLowerCase() method converts a string to lower case
- toUpperCase() method converts a string to upper case
- Why do we use substr() String object method?
Ans. substr() string object method is used to return the characters you specified.
- Syntax: string.substr(start, length)
- Example: (14,7) returns 7 characters, from the 14th character.
- What is the use of the String object method: substring()?
Ans. It returns the characters you specified
- Syntax: string.substring(start, end)
- Example: (7,14) returns ALL characters between the 7th AND the 14th.
- What is the use of math object method: ceil(x)?
Ans. Math method ceil(x) is used to return the next integer Greater Than or Equal to a given number. (That is, rounding up the given number.)
- What is the use of math object method floor(x)?
Ans. Math method floor(x) is used to return the next integer less than or equal to a given number. (That is, rounding down the given number.)
- Which Math method returns the highest-valued number in a list of numbers?
Ans. max(n1, n2, n3,….n)
Where(n1, n2, n3,….n) are the list to compare.
- ____ Math method returns the lowest -valued number in a list of numbers.
Ans. min(n1, n2, n3,….n)
Where (n1, n2, n3,….n) are the list to compare.
- What are the different ways to create new Date object?
Ans.
- var currentdate=new Date();
- var currentdate =new Date(milliseconds);
- var currentdate =new Date(dateString);
- var currentdate =new Date (year, month, day, hours, minute, seconds, milliseconds);
- What is the use of isFixed() Number object method?
Ans. isFixed() Number method is used to return the string that represents a number with exact digits after a decimal point.
- What is the use of Array method indexOf()?
Ans. It searches the array for an element and returns its position.
- Which array method is used to checks whether an object is an array?
Ans. isArray()