Non-primitive types Flashcards

(8 cards)

1
Q

Non-primitive types

A

Primitive data types can store only a single value. To store multiple and complex
values, non-primitive data types are used.
Object - Used to store collection of data.
Example:
// Collection of data in key-value pairs
var obj1 = {
x: 43,
y: “Hello world!”,
z: function(){
return this.x;
}
}
// Collection of data as an ordered list
var array1 = [5, “Hello”, true, 4.1];

Note- It is important to remember that any data type that is not a primitive
data type, is of Object type in javascript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain Hoisting in javascript

A

Hoisting is the default behaviour 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
var hoistedVariable;

Example 2:
hoistedFunction(); // Outputs “ Hello world! “ even when the function is declared afte
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;
}
doSomething(); // Outputs 33 since the local variable “x” is hoisted inside the local
scope
Note - Variable initializations are not hoisted, only variable declarations are
hoisted:
Note - To avoid hoisting, you can run javascript in strict mode by using “use
strict” on top of the code:
“use strict”;
x = 23; // Gives an error since ‘x’ is not declared
var x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why do we use the word “debugger” in javascript?

A

The debugger for the browser must be activated in order to debug the code. Built-in
debuggers may be switched on and off, requiring the user to report faults. The
remaining section of the code should stop execution before moving on to the next
line while debugging

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Difference between “ == “ and “ === “ operators

A

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

Example:
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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Difference between var and let keyword in javascript.

A

Some differences are
1. From the very beginning, the ‘var’ keyword was used in JavaScript programming
whereas the keyword ‘let’ was just added in 2015.
2. The keyword ‘Var’ has function scope. Anywhere in the function, the variable
specified using var is accessible but in ‘let’ the scope of a variable declared with
the ‘let’ keyword is limited to the block in which it is declared. Let’s start with a
Block Scope.
3. ‘var’ declares a variable that will be hoisted but ‘let’ declares a variable that will
be hoisted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain Implicit Type Coercion in javascript.

A

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.
String coercion

String coercion takes place while using the ‘ + ‘ operator. When a number is added to
a string, the number type is always converted to the string type.
Example 1:
var x = 3;
var y = “3”;
x + y // Returns “33”
Example 2:
var x = 24;
var y = “Hello”;
x + y // Returns “24Hello”;
Note - ‘ + ‘ operator when used to add two numbers, outputs a number. The
same ‘ + ‘ operator when used to add two strings, outputs the concatenated
string:
var name = “Vivek”;
var surname = “ Bisht”;
name + surname // Returns “Vivek Bisht”
Let’s understand both the examples where we have added a number to a string,
When JavaScript sees that the operands of the expression x + y are of different types (
one being a number type and the other being a string type ), it converts the number
type to the string type and then performs the operation. Since aer conversion, both
the variables are of string type, the ‘ + ‘ operator outputs the concatenated string
“33” in the first example and “24Hello” in the second example

String coercion

Note - Type coercion also takes place when using the ‘ - ‘ operator, but the
difference while using ‘ - ‘ operator is that, a string is converted to a number
and then subtraction takes place.

var x = 3;
Var y = “3”;
x - y //Returns 0 since the variable y (string type) is converted to a number type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Boolean Coercion

A

Boolean coercion takes place when using logical operators, ternary operators, if
statements, and loop checks. To understand boolean coercion in if statements and
operators, we need to understand truthy and falsy values.
Truthy values are those which will be converted (coerced) to true. Falsy values are
those which will be converted to false.
All values except false, 0, 0n, -0, “”, null, undefined, and NaN are truthy values.
If statements:

Example:

var x = 0;
var y = 23;
if(x) { console.log(x) } // The code inside this block will not run since the value o
if(y) { console.log(y) } // The code inside this block will run since the value of y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly