Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

1

What are the basic datatypes in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

These data types are classified into primitive (basic) and non-primitive (or reference) data types. The primitive data types usually refer to a single value.
JavaScript has five basic data types. They are:
a. String
A string represents a series of characters within double or single quotes.
Example:
let str = “Hello World”
b. Number
The number data type represents numerical value. It refers to both integer and floating point numbers.
Example:
let num=1000
c. Boolean
Boolean is a logical data type which can take only two values. i.e. True or False
Example:
let bool = True
d. Undefined
When a variable is not assigned any value, then it is of Undefined Type. The undefined data type represents undefined or unknown value. It is the default value of undefined variables.
Example:
let year; Here, age is an undefined data type.
e. Null
The Null data type represents null value (no value). It represents ‘empty’ or ‘nothing’.
Example:
let x =null;

LRM_EXPORT_207556595493866_20190724_1939

Q

2

What are the reference datatypes in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

The reference data types are also called non-primitive data types. They usually represent one or more key-value pairs. There are three reference data types in JavaScript. They are:
a. Object
In JavaScript, an object is a collection of data. An object represents both state and behavior. Usually an object exists as a collection of key-value pairs. The properties are associations between a name and a value. The values in the name-value pairs are called methods.
b. Array
An array is a collection of values of the same data type. The elements in the array are indexed. The value of the index starts from 0. In JavaScript arrays are immutable.
Example:
var arr = [40,30,12];
c. RegExp
The RegExp data type represents a regular expression. It describes a specific pattern of characters. It is used for pattern-searching in a string.
Example:
Var pattern = /rootworkz/i i’ is used to search for the given pattern irrespective of its case (case- insensitive)

LRM_EXPORT_207556595493866_20190724_1939

Q

3

What are literals in JavaScript. Explain it's types.

LRM_EXPORT_207556595493866_20190724_1939

Ans

In JavaScript, literals are constant values that are assigned to variables. The literals are also known as constants.
There are different types of literals in JavaScript. They are listed below:
a. Integer Literal
An integer literal represents a numerical value (either positive or negative integer). There should not be any blank space or separators within the integer literal. An integer literal can be represented in different ways:
1. Octal (base 8)
2. Decimal (base 10)
3. Hexadecimal (base 16)
4. Binary (base 2)
Example:
56 (decimal)
o22 (octal)
0xFF (Hexadecimal)
100101 (binary)
b. Floating point Literal
Floating point literals represent floating point values. They have three parts: integral part, decimal point and fractional part. Floating point literals can be represented only in decimal form.
Example: 6.627642
c. Char Literal
A character literal can be represented in one of the following methods:
a. Single quotes
The character literals can be specified within a single quote.
Example: char c= ‘h’
b. Escape sequence
The escape sequences start with a backslash (\).
Example: char ch = ‘\r’
c. Integral value
An integer value can be used as a character literal. The integer value represents the Unicode value of the character.
Example: char ch = 96
d. Unicode
The Unicode representation of the character literal is done in the following format: ‘\uXXXX’ where XXXX represent a hexadecimal number.
Example: char ch=’\u1234’
d. String Literal
A string literal is a series of characters which are specified within double quotes.
Example: string str=”Hello World”
e. Boolean Literal
A Boolean literal can take only two values i.e. either True or False
Example: bool b= False
f. Array Literal
An array literal consists of a list of values of the same type. These values are enclosed in a square bracket.
Example: fruits = [“Orange”, “Mango”, “Banana”]
g. Object Literal
The objects are created using object literals. The object literals contain property names and their values. Syntax: object={property1:value1,property2:value2.....propertyN:valueN}
Example:
var myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969};

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Explain the syntax rules for Javascript literals.

LRM_EXPORT_207556595493866_20190724_1939

Ans

The following are the syntax rules for JavaScript literals:
a. The integer literals should not contain any blank spaces or separators within them.
b. The integer literals should not contain decimal point within them.
c. The string literals must be enclosed within single or double quotes.
d. The string literals must not contain unescaped newline or linefeed characters.
e. The floating point literals must not be in octal or hexadecimal form.
f. The octal integer literals should begin with ‘o’ or ‘O’
g. The hexadecimal integer literals must begin with ‘0x’ or ‘0X’

LRM_EXPORT_207556595493866_20190724_1939

Q

5

What are Objects in JavaScript?

LRM_EXPORT_207556595493866_20190724_1939

Ans

In JavaScript, an object is a collection of data. An object represents both state and behavior. Usually an object exists as a collection of key-value pairs. The properties are associations between a name and a value. The values in the name-value pairs are called methods.
The objects are created using object literals. The object literals contain property names and their values.
Syntax:
object={property1:value1,property2:value2.....propertyN:valueN}
Example:
var myCar = {
make: 'Ford',
model: 'Mustang',
year: 1969 };
An object can also be created using the instance of an object
Syntax:
var objectname=new Object();
Example:
var myCar = new Object();
The properties of an object are accessed using the dot (.) operator.
Syntax:
objectName.propertyName
A function within an object is called method. Method represents the property of an object. A method is defined as shown below:
Syntax:
objectName.methodname = functionName;

var myObj = { myMethod: function(params) {
// ...do something
}
// OR THIS WORKS TOO

myOtherMethod(params)
{
// ...do something else
}
};

LRM_EXPORT_207556595493866_20190724_1939

Q

6

Explain the character set used in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

JavaScript uses the Unicode character set. It is a superset of ASCII and Latin-1 character sets. It is the best method for encoding and representation of all possible characters. The Unicode character set covers almost 154 languages, and several symbol sets including emojis. Usually, UTF-16 encoding is used by JavaScript machines. UTF stands for Unicode Transformation Format. In UTF-16, each code unit is of 16 bit length.

LRM_EXPORT_207556595493866_20190724_1939