What is JavaScript?
JavaScript is a scripting language. It is different from Java
language. It is object-based, lightweight language. It is widely
used for client-side validation.
List some features of JavaScript.
o Lightweight
o Interpreted programming language
o Good for the applications which are network-centric
o Complementary to Java
o Complementary to HTML
o Open source
Cross-platform
Who developed JavaScript, and what
was the first name of JavaScript?
JavaScript was developed by Brendan Eich in September 1995.
At the time of its launch, JavaScript was initially called Mocha.
After that, it was called Live Script and later known as
JavaScript.
List some of the advantages of
JavaScript.
Some of the advantages of JavaScript are:
o Server interaction is less
o Feedback to the visitors is immediate
o Interactivity is high
o Interfaces are richer
List some of the disadvantages of
JavaScript.
Some of the disadvantages of JavaScript are:
o No support for multithreading
o No support for multiprocessing
o Reading and writing of files is not allowed
o No support for networking applications.
15) What are the key differences
between Java and JavaScript? / How
is JavaScript different from Java?
Java JavaScript
Java is a complete and strongly JavaScript is a weakly typed, lightweight
typed programming language programming language (most
used for backend coding. In commonly known as scripting language)
Java, variables must be declared and has more relaxed syntax and rules.
first to use in the program, and
the type of a variable is checked
at compile-time.
Java creates applications that JavaScript code can run only in the
can run in any virtual machine browser, but it can now run on the
(JVM) or browser. server via Node.js.
The Java code needs to be The JavaScript code doesn't require to
compiled. be complied.
Java Objects are class-based. JavaScript Objects are prototype-based.
What are the different data types present in
JavaScript?
There are two types of data types in JavaScript:
o Primitive data types
o Non- Primitive data types
Primitive data types
The primitive data types are as follows:
String: The string data type represents a sequence of characters. It is written within
quotes and can be represented using a single or a double quote.
Example:
1. var str1 = "Hello JavaTpoint"; //using double quotes
2. var str2 = 'Hello Javatpoint'; //using single quotes
Number: The number data type is used to represent numeric values and can be written
with or without decimals.
Example:
1. var x = 5; //without decimal
2. var y = 5.0; //with decimal
Boolean: The Boolean data type is used to represent a Boolean value, either false or
true. This data type is generally used for conditional testing.
Example:
1. var x = 5;
2. var y = 6;
3. var z = 5;
4. (x == y) // returns false
5. (x == z) //returns true
BigInt: The BigInt data type is used to store numbers beyond the Number data type
limitation. This data type can store large integers and is represented by adding "n" to an
integer literal.
Example:
1. var bigInteger = 123456789012345678901234567890;
2. // This is an example of bigInteger.
Undefined: The Undefined data type is used when a variable is declared but not
assigned. The value of this data type is undefined, and its type is also undefined.
Example:
1. var x; // value of x is undefined
2. var y = undefined; // You can also set the value of a variable as undefined.
Null: The Null data type is used to represent a non-existent, null, or a invalid value i.e.
no value at all.
Example:
1. var x = null;
Symbol: Symbol is a new data type introduced in the ES6 version of JavaScript. It is used
to store an anonymous and unique value.
Example:
1. var symbol1 = Symbol('symbol');
typeof: The typeof operator is used to determine what type of data a variable or
operand contains. It can be used with or without parentheses (typeof(x) or typeof x).
This is mainly used in situations when you need to process the values of different types.
Example:
1. typeof 10; // Returns: "number"
2. typeof 10.0; // Returns: "number"
3. typeof 2.5e-4; // Returns: "number"
4. typeof Infinity; // Returns: "number"
5. typeof NaN; // Returns: "number". Despite being "Not-A-Number"
6. // Strings
7. typeof ''; // Returns: "string"
8. typeof 'Welcome to JavaTpoint'; // Returns: "string"
9. typeof '12'; // Returns: "string". Number within quotes is typeof string
10. // Booleans
11. typeof true; // Returns: "boolean"
12. typeof false; // Returns: "boolean"
13. // Undefined
14. typeof undefined; // Returns: "undefined"
15. typeof undeclaredVariable; // Returns: "undefined"
16. // Null
17. typeof Null; // Returns: "object"
18. // Objects
19. typeof {name: "John", age: 18}; // Returns: "object"
20. // Arrays
21. typeof [1, 2, 3]; // Returns: "object"
22. // Functions
23. typeof function(){}; // Returns: "function"
Non-Primitive data types
In the above examples, we can see that the primitive data types can store only a single
value. To store multiple and complex values, we have to use non-primitive data types.
The non-primitive data types are as follows:
Object: The Object is a non-primitive data type. It is used to store collections of data. An
object contains properties, defined as a key-value pair. A property key (name) is always a
string, but the value can be any data type, such as strings, numbers, Booleans, or
complex data types like arrays, functions, and other objects.
Example:
1. // Collection of data in key-value pairs
2. var obj1 = {
3. x: 123,
4. y: "Welcome to JavaTpoint",
5. z: function(){
6. return this.x;
7. }
8. }
Array: The Array data type is used to represent a group of similar values. Every value in
an array has a numeric position, called its index, and it may contain data of any data
type-numbers, strings, Booleans, functions, objects, and even other arrays. The array
index starts from 0 so that the first array element is arr[0], not arr[1].
Example:
1. var colors = ["Red", "Yellow", "Green", "Orange"];
2. var cities = ["Noida", "Delhi", "Ghaziabad"];
3. alert(colors[2]); // Output: Green
4. alert(cities[1]); // Output: Delhi
What is BOM?
BOM stands for Browser Object Model. It provides interaction with the
browser. The default object of a browser is a window. So, you can call
all the functions of the window by specifying the window or directly.
The window object provides various properties like document, history,
screen, navigator, location, innerHeight, innerWidth.
What is DOM? What is the use of document
object?
DOM stands for Document Object Model. A document object
represents the HTML document. It can be used to access and change
the content of HTML.