JavaScript Notes: Data Types

Updated: Jun 2

Mobiprep has created last-minute notes for all topics of Javascript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on javascript.

  1. JavaScript: Introduction

  2. Data Types

  3. Variables

  4. Operators

  5. Functions

  6. Conditional Statements and Loops

  7. Arrays

  8. Objects

  9. DOM Elements

  10. Cookies

  11. Pop Up Box

  12. Exception Handling


Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. What are the basic datatypes in JavaScript?

  2. What are the reference datatypes in JavaScript?

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

  4. Explain the syntax rules for Javascript literals.

  5. What are Objects in JavaScript?

  6. Explain the character set used in JavaScript.

JavaScript Notes: Data Types

Question 1. What are the basic datatypes in JavaScript?

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:

  1. String : A string represents a series of characters within double or single quotes. Example: let str = “Hello World”

  2. Number: The number data type represents numerical value. It refers to both integer and floating point numbers. Example: let num=1000

  3. Boolean: Boolean is a logical data type which can take only two values. i.e. True or False Example: let bool = True

  4. 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.

  5. Null: The Null data type represents null value (no value). It represents ‘empty’ or ‘nothing’. Example: let x =null;

 

Question 2. What are the reference datatypes in JavaScript?

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)

 

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

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:

  1. Single quotes The character literals can be specified within a single quote. Example: char c= ‘h’

  2. Escape sequence The escape sequences start with a backslash (\). Example: char ch = ‘\r’

  3. 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

  4. 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};

 

Question 4. Explain the syntax rules for Javascript literals.

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’

 

Question 5. What are Objects in JavaScript?

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
  }
};

 

Question 6. Explain the character set used in JavaScript.

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.

 



Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB

 

Mobiprep is a one stop solution for all exam preperation needs. Get all exam prep resources at a single platform.

Back to Top

 


13 views0 comments

Recent Posts

See All