Default Flashcards Preview

JavaScript > Default > Flashcards

Flashcards in Default Deck (45)
Loading flashcards...
1
Q
var myArray = [];
myArray[0] = "Val1";
myArray[2] = "Val3";

What is the value of myArray[1]?

A

undefined

myArray[1] == undefined

2
Q
var customer = {
name: "Lidiya"
};

What is the value of customer.Age?

A

undefined

customer.Age == undefined

3
Q

What operator can be used to check the type of a variable?

A

typeof

4
Q

var myName = “Myro”;

console.log( _______ myName);

What operator should be used in front of myName in the second line to get the type of myName to show in the console?

A

typeof

console.log( typeof myName);

5
Q

var myAge = 38 ;

console.log(typeof myAge);

What will be displayed in the console?

A

number

6
Q

When is null used in JavaScript?

A

In places where the object should be but can’t be created of found. For example, if you try the following:

var myObj = document.getElementById(“elm1”);

and elm1 id does not exist, myObj will be set to null.

7
Q

When is undefined used in JavaScript?

A

For the variable that is not initialised, an object’s missing property or a missing value of an array.

8
Q
var test10 = null;
console.log(typeof test);

What will be output to console?

A

object

9
Q
var test = 0/0;
console.log(test);

What will be output to console?

A

NaN

10
Q
var test = "food" * 10;
console.log(test);

What will be output to console?

A

NaN

11
Q

How to check whether something has NaN value?

A

isNan() function

For example:

var myNum = 0;
if(isNaN(myNum)){
    //some code
}
12
Q
var test11 = 0 / 0;
console.log(typeof test11);

What will be output to console?

A

number

13
Q

What happens when you compare a string to a number?

99 == “bob”

A

The string is converted to a number and then two numbers are compared. In this case “bob” converted to a number is NaN and 99 not equals to NaN.

14
Q

What number does boolean value “true” convert to?

A

1

15
Q

What number does boolean value “false” convert to?

A

0

16
Q

Ho will this comparison proceed?

1 == true

A

“true” will be converted to number 1 and compared to 1. The result will be true

17
Q

How will this comparison proceed?

“1” == true

A
  1. “true” will be converted to number 1
    “1” == 1
  2. “1” will be converted to number
    1 == 1
18
Q

is this true?

undefined == null

A

Yes! That is the rule in JavaScript.

19
Q

What number is the empty string converted to in this case?

1 == “”

A

”” is converted to number 0

20
Q

How will this comparison proceed?

“true” == true

A
  1. boolean true will be converted to a number 1
  2. “true” will be attempted to be converted to a number and the left side will get a value of NaN
  3. NaN == 1? false
21
Q

Is there a !== operator?

A

Yes!

22
Q
var add = "4" + 3;
console.log(add);

What will be output to console?

A

43

”+” converts the number into the string first, them concatenates

23
Q
var add = 78+ "2";
console.log(add);

What will be output to console?

A

782

”+” converts the number into the string first, them concatenates

24
Q
var multi= 4 * "2";
console.log(multi);

What will be output to console?

A

8

In case of all arithmetic operators besides “+”, the string is first converted to number and then the operation is performed.

25
Q
var minus= "7" - 2;
console.log(minus);

What will be output to console?

A

5

In case of all arithmetic operators besides “+”, the string is first converted to number and then the operation is performed.

26
Q
var order = 1 + 2 + " pizzas";
console.log(order);

What will be output to console?

A

3 pizzas

”+” operator has left-to-right associativity

27
Q
var order = 1 + (2 + " pizzas");
console.log(order);

What will be output to console?

A

12 pizzas

concatenating string with number always converts number to string first

28
Q

What function is used to convert string to number?

A

Number()

For example:

var num = 3 + Number(“4”);

num will be set to 7

29
Q
var res= "Result: " + 10/2;
console.log(res);

What will be output to console?

A

Result: 5

The division will happen first because of the operators' order of precedence:
"Result: " + (10/2)
          | |
          V
"Result: " + 5
30
Q

What are 5 falsey values in JavaScript?

A
  1. undefined
  2. null
  3. 0 (zero)
  4. empty string
  5. NaN
31
Q
if([]){
console.log("This is true");
}
else{
console.log("This is false");
}

What will be output to console?

A

This is true.

[] is an empty array and it is not one of the falsey values.

32
Q
if(" "){
console.log("This is true");
}
else{
console.log("This is false");
}

What will be output to console? Note that this is a string with one space in it.

A

This is true.

This is a non-empty string, therefore it is truthy.

33
Q

var uppCase = “HELLO”;

How to put a text in variable uppCase into lower case?

A

use toLowerCase() methodof a string type, like so:

alert(uppCase.toLowerCase());

34
Q

var emailVar = “hello@gmail.com”;

How can we get a string containing “@” from variable emailVar?

A

var newStr = emailVar.charAt(5);

35
Q
var strVar = "I like movies";
console.log(strVar.indexOf("mov"));

What will be output to console?

A

7

indexOf() function of the string object returns the index of of the first character of the first occurrence of the argument.

36
Q
var strVar = "I like to move it move it.";
console.log(strVar.indexOf("mov"));

What will be output to console?

A

10

Although, indexOf() returns the index of the first character of the argument, it only returns the index of the first character in the FIRST OCCURRENCE of the argument.

37
Q

var strVar = “I like to move it move it.”;

How can you get the index of the second occurrence of “move” in strVar?

A

Use indexOf() method of string object with the second parameter. Second parameter is an integer that indicates where in the string should the search begin.

console.log(strVar.indexOf(“mov”, 11));

Guess what would be output to console?

38
Q
var strVar = "I like to move it move it.";
console.log(strVar.indexOf("bleh"));

What will be output to console?

A

-1

indexOf() method returns -1 if the string is not found

39
Q

var strVar = “car/house/road”;

How will you extract “house” string from strVar variable?

A

var extracted = strVar.substring(4,9);

40
Q

var strVar = “car/house/road”;

How will you extract “house/road” string from strVar variable?

A

var extracted = strVar.substring(4);

substring() method of string object, if used with only one parameter, returns the string that begins at the first parameter’s index and ends at the end of the string.

41
Q

var people = “Jack,Jane,John”;

What method of the sting object can we use to put each person into an element in array in one operation?

A

split()

var personArr = people.split(“,”);

42
Q

What are two methods of string object that can be used to get a portion of the string from the original string?

A
  1. slice()

2. substring()

43
Q

What method of string object is used to get the last index of a substring in the original string?

A

lastIndexOf()

44
Q

What method of string object is used to add another string to the current one?

A

concat()

45
Q

How can you get a user input from a browser dialogue?

A

prompt() method.

This is window’s method. You can use either prompt() or window.prompt().

var userInput = prompt(“What is your name?”);