Sunday, 21 February 2021

IP UNIT-2 TWO MAKRS WITH ANSWERS

 What is JavaScript?

JavaScript is a lightweightinterpreted programming language with object-oriented capabilities that allows you to build interactivity into otherwise static HTML pages. JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages and is understood by web browsers.

What are the data types supported by JavaScript?

The data types supported by JavaScript are:

  • Undefined
  • Null
  • Boolean
  • String
  • Symbol
  • Number
  • Object

What are the features of JavaScript?

Following are the features of JavaScript:

  • It is a lightweight, interpreted programming language.
  • It is designed for creating network-centric applications.
  • It is complementary to and integrated with Java.
  • It is an open and cross-platform scripting language.

Is JavaScript a case-sensitive language?

Yes, JavaScript is a case sensitive language.  The language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters.

What are the advantages of JavaScript?

Following are the advantages of using JavaScript −

  • Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
  • Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.
  • Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.
  • Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

How can you create an object in JavaScript?

JavaScript supports Object concept very well. You can create an object using the object literal as follows −

varemp = {
name: "Daniel",
age: 23
};

How can you create an Array in JavaScript?

You can define arrays using the array literal as follows-

var x = [];
var y = [1, 2, 3, 4, 5];

What are the scopes of a variable in JavaScript?

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

  • Global Variables − A global variable has global scope which means it is visible everywhere in your JavaScript code.
  • Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

What is the purpose of ‘this’ operator in JavaScript?

The JavaScript this keyword refers to the object it belongs to. This has different values depending on where it is used. In a method, this refers to the owner object and in a function, this refers to the global object.

What is Callback?

callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘call back‘. 

How to create a cookie using JavaScript?

The simplest way to create a cookie is to assign a string value to the document. Cookie object, which looks like this-

document.cookie = "key1 = value1; key2 = value2; expires = date";

In how many ways a JavaScript code can be involved in an HTML file?

There are 3 different ways in which a JavaScript code can be involved in an HTML file:

  • Inline
  • Internal
  • External

What are the ways to define a variable in JavaScript?

The three possible ways of defining a variable in JavaScript are:

  • Var – The JavaScript variables statement is used to declare a variable and, optionally, we can initialize the value of that variable. Example: var a =10; Variable declarations are processed before the execution of the code.
  • Const – The idea of const functions is not allow them to modify the object on which they are called. When a function is declared as const, it can be called on any type of object.

Let – It is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in.

What is a Typed language?

Typed Language is in which the values are associated with values and not with variables. It is of two types:

  • Dynamically: in this, the variable can hold multiple types; like in JS a variable can take number, chars.
  • Statically: in this, the variable can hold only one type, like in Java a variable declared of string can take only set of characters and nothing else.

What is the difference between window & document in JavaScript?

Window

Document

JavaScript window is a global object which holds variables, functions, history, location.

The document also comes under the window and can be considered as the property of the window.


What is NaN in JavaScript?
NaN is a short form of Not a Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. When a string or something else is being converted into a number and that cannot be done, then we get to see NaN.

What is the DOM?
The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects.
API = DOM + JavaScript

List out the various methods of Document object.
  • write(“string”): writes the given string on the document.
  • 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.
How exceptions are handled in javascript?
By the help of try/catch block, we can handle exceptions in JavaScript. JavaScript supports try, catch, finally and throw keywords for exception handling.

What are the javascript built-in objects?
JavaScript has several built-in or core language objects. These built-in objects are available regardless of window content and operate independently of whatever page your browser has loaded.

Mention the methods of Date object.
  • Date() - Returns today's date and time
  • getDate() - Returns the day of the month for the specified date
  • getDay() - Returns the day of the week for the specified date
  • getFullYear() - Returns the year of the specified date
  • getHours() - Returns the hour in the specified date according to local time.
  • getMilliseconds() - Returns the milliseconds in the specified date according to local time.
What are regular expressions in javascript?
Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match()matchAll()replace()search(), and split() methods of String.

How is javascript used for validation?
JavaScript provides a way to validate form's data on the client's computer before sending it to the web server. Form validation generally performs two functions.
  • Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data.
  • Data Format Validation − Secondly, the data that is entered must be checked for correct form and value. Your code must include appropriate logic to test correctness of data.
What is an event?
JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of events which can trigger JavaScript Code.

What is DHTML?
Dynamic HTML, or DHTML, is a collection of technologies used together to create interactive and animated websites by using a combination of a static markup language (such as HTML), a client-side scripting language (such as JavaScript), a presentation definition language (such as CSS), and the Document Object Model (DOM).

What is JSON?
JSON is the abbreviation of JavaScript Object Notation. It is one of the simplest data interchange format. It is also independent of programming language and platform. Its lightweight text-based structure makes it easily readable by a human. It is derived from JavaScript for presenting simple data in the form of key-value pairs.

What is meant by JSON objects?
An object is defined as a set of key-value pairs. A JSON starts with a left brace “{“ and ends with another right brace “}”. Every key is followed by a colon “:” and the key-value pairs are separated from each other by using a comma “,”. So, basically, JSON object is a collection of keys along with their values arranged in a pre-specified JSON format.

What is the extension of the JSON file?
A JSON file has an extension of “.json”. Being in a text-based format, a JSON file can be viewed or edited using any text editor like notepad or notepad++.

What are the advantages and features of JSON?
  • Easy to use and fast nature. JSON syntax offers easy parsing of data and even faster implementation. The light-weight structure of JSON allows it to respond at a much faster rate.
  • Compatible with numerous operating systems and browsers. This allows JSON schema to be attuned to many platforms without requiring any extra effort to make sure its compatibility with another platform.
  • Supports a wide range of data types including integers, double, String, Boolean, etc.
What are the limitations of JSON?
  • As the data gets complex with several nested or hierarchical structures, it becomes complex for human readability.
  • JSON is not suitable for handling very complex large data.
  • JSON doesn’t have support for handling multimedia formats such as rich text or images.
  • It doesn’t support comments.
What are the uses of JSON?
  • JSON is mainly used for data interchange between two systems.
  • JSON is prominently used for transmission of serialized data over a network connection between two systems.
  • APIs and web services use JSON to format and transfer data.
  • JSON can be used in combination with most of the modern programming languages.
  • JSON can be used with JavaScript applications such as browser plugins and websites.
  • JSON can be used to read data from the web server and display data on the web pages.
Explain JSON syntax rules?
There are several rules that describe the structure of the JSON. They are:
  • Data inside a JSON is arranged in Key value pair. The left side represents the key and the data on the right side represents value. Both key and value are separated by a colon “:”.
  • Each set of key-value pair is separated from the other pair by using a comma “,”.
  • Curly braces define the JSON objects. Left curly brace “{“ represents the start of the object and right curly brace “}” represents the end of an object.
  • Arrays are defined inside a JSON object by using square brackets “[ ]”.
What are the advantages of JSON over XML?
  • JSON is lighter and faster than the XML.
  • JSON has object types but XML doesn’t define objects as types. JSON has different object type for a different set of data such as string, integer, Boolean, array, etc. All XML objects are categorized as just one data type, i.e. string.
  • JSON data can be easily accessed as a JSON object using JavaScript. On the other hand, the XML data need to be parsed and allocated to the variables using APIs. Getting value out of a JSON is as easy as reading an object from your JavaScript programming.
Name the browsers that support JSON format?
Support for JSON is included in almost all the new versions of the browsers. Internet Explorer, Chrome, Safari, Mozilla Firefox, etc. all support JSON format.