JS All Flashcards
What is JavaScript?
JavaScript is a scripting language. Different from Java. It is object-based, lightweight, cross-platform translated language. It is widely used for client-side validation. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.
List some features of JavaScript
Interpreted programming language
Good for the applications which are network-centric
Complementary to Java
Complementary to HTML
Open source
Cross-platform
Who developed JavaScript
Developed by Brendan Eich, 1995 Mocha - Live Script - Java Script
Some of the advantages of JavaScript.
Server interaction is less
Feedback to the visitors is immediate
Interactivity is high
Interfaces are richer
Some of the disadvantages of JavaScript.
No support for multithreading
No support for multiprocessing
Reading and writing of files is not allowed
No support for networking applications.
Define a named function in JavaScript.
The function which has named at the time of definition is called a named function.
~~~
function msg()
{
document.writeln(“Named Function”);
}
msg();
~~~
Name the types of functions
The types of function are:
Named - These type of functions contains name at the time of definition.
~~~
function display()
{
document.writeln(“Named Function”);
}
display();
Anonymous - These type of functions doesn’t contain any name. They are declared dynamically at runtime.
var display=function()
{
document.writeln(“Anonymous Function”);
}
display();
~~~
Define anonymous function
It is a function that has no name. These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration. It can be easily used in the place of an expression.
~~~
var display=function()
{
alert(“Anonymous Function is invoked”);
}
display();
~~~
Can an anonymous function be assigned to a variable?
Yes, you can assign an anonymous function to a variable.
10) In JavaScript what is an argument object?
The variables of JavaScript represent the arguments that are passed to a function.
Closure.
In JavaScript, we need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.
~~~
var num = 10;
function sum()
{
document.writeln(num+num);
}
sum();
~~~
If we want to return the character from a specific index which method is used?
charAt() method is used to find out a char value present at the specified index. The index number starts from 0 and goes to n-1, where n is the length of the string. The index value can’t be a negative, greater than or equal to the length of the string. For example:
~~~
var str=”Javatpoint”;
document.writeln(str.charAt(4));
~~~
Difference between JavaScript and JScript?
JScript is the same as JavaScript, but Microsoft provides it.
hello world example of JavaScript?
You need to place it inside the body tag of HTML.
~~~
document.write("JavaScript Hello World!");
~~~
What are the key in Java?
Java is a complete and strongly typed programming language used for backend coding. In Java, variables must be declared first to use in the program, and the type of a variable is checked at compile-time.
Java is an object-oriented programming (OOPS) language or structured programming languages such as C, C++, or .Net.
Java creates applications that can run in any virtual machine (JVM) or browser.
The Java code needs to be compiled.
Java Objects are class-based. You can’t make any program in Java without creating a class.
Java is a Complete and Standalone language that can be used in backend coding.
Java programs consume more memory.
The file extension of the Java program is written as “.Java” and it translates source code into bytecodes which are then executed by JVM (Java Virtual Machine).
Java supports multithreading.
Java uses a thread-based approach to concurrency.
What are the key in Java Script?
JS is a weakly typed, lightweight programming language and has more relaxed syntax and rules.
JS is a client-side scripting language, and it doesn’t fully support the OOPS concept. It used to make web pages interactive .
JS code can run only in the browser, but it can now run on the server via Node.js.
The JS code doesn’t require to be complied.
JS Objects are prototype-based.
JS is assigned within a web page and integrates with its HTML content.
JS code is used in HTML web pages and requires less memory.
The JS file extension is written as “.js” and it is interpreted but not compiled. Every browser has a JS interpreter to execute the JS code.
JS doesn’t support multithreading.
JS uses an event-based approach to concurrency.
How to use external JavaScript file?
js file name is message.js, place the following script tag inside the head tag.
~~~
~~~
Is JavaScript case sensitive language?
Yes, JavaScript is a case sensitive language.
~~~
Var msg = “JavaScript is a case-sensitive language”; //Here, var should be used to declare a variable
function display()
{
document.writeln(msg); // It will not display the result.
}
display();
~~~
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.
What is the use of window object?
The window object is created automatically by the browser that represents a window of a browser. It is not an object of JavaScript. It is a browser object.
The window object is used to display the popup dialog box.
Method Description
alert() displays the alert box containing the message with ok button.
confirm() displays the confirm dialog box containing the message with ok and cancel button.
prompt() displays a dialog box to get input from the user.
open() opens the new window.
close() closes the current window.
setTimeout() performs the action after specified time like calling function, evaluating expressions.
What is the use of history object?
The history object of a browser can be used to switch to history pages such as back and forward from the current page or another page. There are three methods of history object.
history.back() - It loads the previous page.
history.forward() - It loads the next page.
history.go(number) - The number may be positive for forward, negative for backward. It loads the given page number.
How to write a comment in JavaScript?
There are two types of comments in JavaScript.
Single Line Comment: It is represented by // (double forward slash)
Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */
How to create a function in JavaScript?
Create a function in JavaScript, follow the following syntax.
~~~
function function_name(){
//function body
}
~~~
What are the different data types present in JavaScript?
There are two types of data types in JavaScript:
Primitive data types
Non- Primitive data types
Primitive data types String
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.
~~~
var str1 = “Hello JavaTpoint”; //using double quotes
var str2 = ‘Hello Javatpoint’; //using single quotes
~~~
Primitive data types Number
Number: The number data type is used to represent numeric values and can be written with or without decimals.
~~~
var x = 5; //without decimal
var y = 5.0; //with decimal
~~~
Primitive data types Boolean
Boolean: The Boolean data type is used to represent a Boolean value, either false or true. This data type used for conditional testing.
var x = 5; var y = 6; var z = 5; (x == y) // returns false (x == z) //returns true
Primitive data types BigInt
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.
~~~
var bigInteger = 123456789012345678901234567890;
// This is an example of bigInteger.
~~~
Primitive data types Undefined
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.
~~~
var x; // value of x is undefined
var y = undefined; // You can also set the value of a variable as undefined.
~~~
Primitive data types Null
Null: The Null data type is used to represent a non-existent, null, or a invalid value i.e. no value at all .
var x = null;
Primitive data types Symbol
Symbol: Used to store an anonymous and unique value.
~~~
var symbol1 = Symbol(‘symbol’);
~~~
What is typeof?
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.
```typeof 10; // Returns: “number”
typeof 10.0; // Returns: “number”
typeof 2.5e-4; // Returns: “number”
typeof Infinity; // Returns: “number”
typeof NaN; // Returns: “number”. Despite being “Not-A-Number”
// Strings
typeof ‘’; // Returns: “string”
typeof ‘Welcome to JavaTpoint’; // Returns: “string”
typeof ‘12’; // Returns: “string”. Number within quotes is typeof string
// Booleans
typeof true; // Returns: “boolean”
typeof false; // Returns: “boolean”
// Undefined
typeof undefined; // Returns: “undefined”
typeof undeclaredVariable; // Returns: “undefined”
// Null
typeof Null; // Returns: “object”
// Objects
typeof {name: “John”, age: 18}; // Returns: “object”
// Arrays
typeof [1, 2, 3]; // Returns: “object”
// Functions
typeof function(){}; // Returns: “function”
~~~
Non-Primitive data types Object
Object: The Object is a non-primitive data type, 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.
~~~
// Collection of data in key-value pairs
var obj1 = {
x: 123,
y: “Welcome to JavaTpoint”,
z: function(){
return this.x;
}
}
~~~
Non-Primitive data types Array
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].
~~~
var colors = [“Red”, “Yellow”, “Green”, “Orange”];
var cities = [“Noida”, “Delhi”, “Ghaziabad”];
alert(colors[2]); // Output: Green
alert(cities[1]); // Output: Delhi
~~~