JavaScript is a client-side and server-side (NodeJS) scripting language inserted into HTML pages. It is understood by web browsers. JavaScript is also an Object-based Programming language, dynamic typed and case sensitive.
Note: It is important to remember that any data type that is not a primitive data type, is of "Object type" in JS.
Examples:
Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.
(Add Example)
Hoisting is the default behavior of javascript where all the variable and function declarations are moved on top. This means that irrespective of where the variables and functions are declared, they are moved on top of the scope. The scope can be both local and global.
Example 1:
hoistedVariable = 3;
console.log(hoistedVariable); // Outputs 3, even when the variable is declared after it is initialized.
var hoistedVariable;
Example 2:
hoistedFunction(); // Outputs " Hello world! ", even when the function is declared after calling.
function hoistedFunction(){
console.log(" Hello world! ");
}
Example 3:
// Hoisting takes place in the local scope as well.
function doSomething(){
x = 33;
console.log(x);
var x;
}
Example 4:
var x;
console.log(x); // Outputs "undefined" since the initialization of "x" is not hoisted.
x = 23;
Example 5:
"use strict";
x = 23; // Gives an error since 'x' is not declared.
var x;
Implicit type coercion in javascript is the automatic conversion of value from one data type to another. It takes place when the operands of an expression are of different data types.
JS is a dynamically typed language because the type of a variable is checked during run-time in contrast to a statically typed language, where the type of a variable is checked during compilation-time. Since JS is a loosely (dynamically) typed language, variables in JS are not associated with any type. A variable can hold the value of any data type.
The parseInt() function is used to convert numbers between different bases. parseInt() takes the string to be converted as its first parameter. The second parameter is the base of the given string. To convert 4F (or base 16) to integer, the code used will be:
parseInt("4F", 16);
Both are comparison operators. The difference between both the operators is that “==” is used to compare values whereas, “ === “ is used to compare both values and types.
var x = 2;
var y = "2";
(x == y) // Returns true since the value of both x and y is the same
(x === y) // Returns false since the typeof x is "number" and typeof y is "string"
isNan function returns true if the argument is not a number; otherwise, it is false.
NaN property represents the “Not-a-Number” value. It indicates a value that is not a legal number.
typeof of NaN will return a Number.
To check if a value is NaN, we use the isNaN() function,
Note: isNaN() function converts the given value to a Number type, and then equates to NaN.
Undeclared variables are those that do not exist in a program and are not declared. If the program tries to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any value. If the program tries to read the value of an undefined variable, an undefined value is returned.
The NULL value is used to represent no value or no object. It implies no object or null string, no valid boolean value, no number, and no array object.
Undefined value means the:
A function that is declared without any named identifier is known as an anonymous function. In general, an anonymous function is inaccessible after its declaration.
var anonymous = function() {
alert('I am anonymous');
};
anonymous();
Yes, JavaScript is case-sensitive. For example, a function parseInt is not the same as the function Parseint.
DOM stands for "Document Object Model" and is responsible for how various objects in a document interact with each other. DOM is required for developing web pages, which includes objects like paragraphs, headings, links, etc. These objects can be operated to include actions like add or delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over other existing models.
HTML events are "things" that happen to HTML elements. When JS is used in HTML pages, JS can "react" on these events.
Events are the actions that result from activities:
An event handler is required to manage the proper execution of all these events. Event handlers are an extra attribute of the object. This attribute includes the event’s name and the action taken if the event takes place.
<button onclick="this.innerHTML = Date()">What time is it?</button>
ECMA means "European Computer Manufacturers Association". It is a non-profit organization that develops standards in computer hardware, communications, and programming languages.
ECMAScript is a JS standard meant to ensure the interoperability of web pages across different web browsers. It is standardized by Ecma International according to the document ECMA-262.
ECMA Script is like rules and guidelines, while JS is a scripting language used for web development.
Suppose you have only a few lines of code that is specific to a particular webpage. In that case, it is better to keep your JS code internal within your HTML document.
On the other hand, if your JS code is used in many web pages, you should consider keeping your code in a separate file. If you wish to make some changes to your code, you have to change only one file, making code maintenance easy. If your code is too long, it is better to keep it in a separate file. This helps in easy debugging.