Variables and data types Flashcards

1
Q

JS in loosely typed. What does that mean?

A

It means that a variable can be defined with just a name. Things like lenght and type are not declared

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

Are variables case sensitive

A

Yes

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

What must variables begin with

A

a character

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

What value will an variable with an unassigned value return?

A

Undefined

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

How many primitive data types are in JS?

A

Undefined
Number
String
Boolean
Object
Structural type

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

Give an example of an undefined data type

A

Var car;

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

Give an example of a number data type

A

var myweight = 20;

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

Give an example of a string data type

A

var dogname = “Isa”;

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

Give an example of a boolean data type

A

var theswitch = false;

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

Give an example of a number array data type

A

varnumarray = [1,3,5,7,9];

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

Give an example of an object data type

A

var myobj = {firstname:”Bob”,lastname:”Heide”};

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

Why are JS types considered dynamic?

A

Because the type changes based on the value of the var. For example var myname = “bob”; is a strin but I can then say myname = 3; and that changes the type to numeric

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

How can i display the type of a variable?

A

console.log(typeof myname);

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

What is the data type of the following statement
var fruit = “apple” + 3;

A

The type is string because it is set by the first part of the value which is a string

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

What would the value of the variable be? What would it’s type be?

var mynum = 5 + 3 + “gift”;

A

The answer is 8gift.
JavaScript moves left to right. In this case the numbers are treated as math and then the string is added. This makes the type string

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

What is Global scope in JS?

A

A variable defined outside of an actual function. For example
var car = “BMW”;
Function showCar( ) {
console.log(car);
}
showCar( );
Car is a global variable available in the function

17
Q

What is local scope in JS?

A

function showCar() {
var car = “BWM”;
console.log(car);
}
showCar(); /*will list the car /
console.log(car); /
this will fail because car was created as a local var not a global. Outside of the function it does not exist

18
Q

What is meant by local scope:block scope?

A

Think of this like the “let” statement in ABAP. The block scope is local to the keyword if you will. For example
function test() {
if (true)
var1 = “BMW”;
let car2 = “AUDI”;
}
In the above scenario car2 is only valid within the if statement in which it is declared

19
Q

In modern syntax what two new ways of declaring variables exist?

A

let and const
These allow us to create block scope variables

20
Q

What is the difference between let and const

A

think of const as a constant in ABAP. It is declared with a value and the value cannot be changed.
const firstName = “John” will always be John. You can’t then say
firstName = “Bob”

21
Q

What string method returns the length of the string?

A

let alpha = “abcdef”;
console.log(‘String length is ${alphabet.length}’);

22
Q

What string method finds a value in a string?

A

let alpha = “Where ever you go there you are”;
console.log(‘Go lies at index: ${alpha.indexOf(“go”)}’);

23
Q

What method allows the reduction of decimal places?

A

let num1 = 5.1234;
console.log(num1.toFixed(2)); // 5.12

24
Q

How can a string be converted to a number and vice versa

A

let num1 = 14.33
console.log(typeof num1);
let numString = num1.toString();
let num2 = Number(num1)
-this example is from page 23 of the book and makes little sense