What is JavaScript?
JavaScript is a lightweight, interpreted 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?
A 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. |
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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 “[ ]”.
- 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.