Javascript Basics Flashcards

1
Q

Name the three ways to declare a variable?

A

In JavaScript, we can declare a variable in different ways by using different keywords. Each keyword holds some specific reason or feature in JavaScript. Basically we can declare variables in three different ways by using var, let and const keyword. Each keyword is used in some specific conditions.
var: This keyword is used to declare variable globally. If you used this keyword to declare variable then the variable can accessible globally and changeable also. It is good for a short length of codes, if the codes get huge then you will get confused.

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

What rules should you follow when naming variables?

A

Variable names cannot contain spaces.

Variable names must begin with a letter, an underscore (_) or a dollar sign ($).

Variable names can only contain letters, numbers, underscores, or dollar signs.

Variable names are case-sensitive.

Certain words may not be used as variable names, because they have other meanings within JavaScript.

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

How does the % operator work?

A

An operator performs some operation on single or multiple operands (data value) and produces a result. … For example, in 1 + 2 , the + sign is an operator and 1 is left side operand and 2 is right side operand. The + operator performs the addition of two numeric values and returns a result.

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

Explain the difference between == and ===.

A

= is used for assigning values to a variable in JavaScript. == is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

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

When would you receive a NaN result?

A

You get NaN when the value cannot be computed or as a result of attempted number coercion (type conversion) of non-numeric value (such that undefined) for which primitive numeric value is not available

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

How do you increment and decrement a number?

A

JavaScript has an even more succinct syntax to increment a number by 1. The increment operator ( ++ ) increments its operand by 1 ; that is, it adds 1 to the existing value. There’s a corresponding decrement operator ( – ) that decrements a variable’s value by 1 . That is, it subtracts 1 from the value

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

Explain the difference between prefixing and post-fixing increment/decrement operators.

A

Increment Operator adds 1 to the operand. Decrement Operator subtracts 1 from the operand. Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased).

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

What are Increment Operators?

A

The increment operator is used to increment the value of a variable in an expression. In the Pre-Increment, value is first incremented and then used inside the expression. Whereas in the Post-Increment, value is first used inside the expression and then incremented.
Syntax:

// PREFIX
\++m
// POSTFIX
m++

where m is a variable

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

What are Decrement Operators?

A

The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, value is first decremented and then used inside the expression. Whereas in the Post-Decrement, value is first used inside the expression and then decremented.
Syntax:

// PREFIX
--m
// POSTFIX
m--

where m is a variable

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

What is operator precedence and how is it handled in JS?

A

Operator precedence determines the order in which operators are evaluated. Operators with higher precedence are evaluated first. The multiplication operator (“ * “) has higher precedence than the addition operator (“ + “) and thus will be evaluated first.

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

How do you access developer tools and the console?

A

To open the developer console in Google Chrome, open the Chrome Menu in the upper-right-hand corner of the browser window and select More Tools > Developer Tools. You can also use the shortcut Option + ⌘ + J (on macOS), or Shift + CTRL + J (on Windows/Linux).

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

How do you log information to the console?

A

Click the Console tab.
Press Control + [ or Command + [ (Mac) until the Console panel is in focus.
Open the Command Menu, start typing Console , select the Show Console Panel command, and then press Enter .

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

What are the eight data types in JavaScript?

A
String.
Number.
Boolean.
Null.
Undefined.
Symbol.
BigInt.
Object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which data type is NOT primitive?

A

The ‘object’ is a non-primitive data type in JavaScript. Arrays and Functions in JavaScript belong to the ‘object’ data type

1
var obj1 = { a: 5, b: 6 };
We can change or mutate the value of obj1. 

1
2
obj1[a] =7;
console.log(obj1) // will return the value {a: 7, b: 6}

Thus the value has changed successfully.

When we check the value of obj1 using the typeof operator, it returns an object.

1
typeof (obj1) // will return the data type ‘object’.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the relationship between null and undefined?

A

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value

Undefined example.

var demo;
alert(demo); //shows undefined
alert(typeof demo); //shows undefined

Null example

var demo = null;
alert(demo); //shows null
alert(typeof demo); //shows object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between single, double, and backtick quotes for strings?

A

There is only one difference between the use of single quotes ( ‘’ ) and double quotes ( “” ) in JavaScript, and it comes down to which quote character you have to escape using the backslash ( \ ) character: ' or "

17
Q

What is the term for embedding variables/expressions in a string?

A

JavaScript string interpolation is the process of embedding an expression into part of a string. A template literal is used to embed expressions. You can add values such as variables and mathematical calculations into a string using interpolation

18
Q

Which type of quote lets you embed variables/expressions in a string?

A

Strings in JavaScript are contained within a pair of either single quotation marks ‘’ or double quotation marks “”. Both quotes represent Strings but be sure to choose one and STICK WITH IT. If you start with a single quote, you need to end with a single quote.

19
Q

What is the difference between slice/substring/substr?

A

What it does? The slice() method extracts parts of a string and returns the extracted parts in a new string. The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.

in Summary:
if you know the index(the position) on which you’ll stop (but NOT include), Use slice()
if you know the length of characters to be extracted use substr()

20
Q

What are the three logical operators and what do they stand for?

A

JavaScript supports three logical operators: AND, OR, and NOT. These can be used to “reason” about Booleans.

The && operator represents logical and. It is a binary operator, and its result is true only if both the values given to it are true.

1
console.log(true && false)
2
// → false
3
console.log(true && true)
4
// → true
The || operator denotes logical or. It produces true if either of the values given to it is true.
console.log(false || true)
// → true
console.log(false || false)
// → false
Not is written as an exclamation mark (!). It is a unary operator that flips the value given to it—!true produces false, and !false gives true.

When mixing these Boolean operators with arithmetic and other operators, it is not always obvious when parentheses are needed. In practice, you can usually get by with knowing that of the operators we have seen so far, || has the lowest precedence, then comes &&, then the comparison operators (>, ==, and so on), and then the rest. This order has been chosen such that, in typical expressions like the following one, as few parentheses as possible are necessary:

1 + 1 == 2 && 10 * 10 > 50
The last logical operator I will discuss is not unary, not binary, but ternary, operating on three values. It is written with a question mark and a colon, like this:

console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2
This one is called the conditional operator (or sometimes just the ternary operator since it is the only such operator in the language). The value on the left of the question mark “picks” which of the other two values will come out. When it is true, it chooses the middle value, and when it is false, it chooses the value on the right.
21
Q

What are the comparison operators?

A

Given that x = 5,

the table below explains the comparison operators:

Operator Description Comparing Returns Try it
== equal to x == 8 false
x == 5 true
x == “5” true
=== equal value and equal type x === 5 true
x === “5” false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== “5” true
x !== 8 true
> greater than x > 8 false
< less than x < 8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true

22
Q

What are truthy and falsy values?

A

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false , 0 , -0 , 0n , “” , null , undefined , and NaN ). JavaScript uses type coercion in Boolean contexts.

Examples of truthy values in JavaScript (which will be coerced to true in boolean contexts, and thus execute the if block):

if (true)
if ({})
if ([])
if (42)
if ("0")
if ("false")
if (new Date())
if (-42)
if (12n)
if (3.14)
if (-3.14)
if (Infinity)
if (-Infinity)
23
Q

What are the falsy values in JavaScript?

A

A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined , null , NaN , 0 , “” (empty string), and false of course.

if (!variable) {
  // When the variable has a falsy value the condition is true.
}
General Examples
var string = ""; //
24
Q

What are conditionals?

A

Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run. There are multiple different types of conditionals in JavaScript including: “If” statements: where if a condition is true it is used to specify execution for a block of code.“Else” statements: where if the same condition is false it specifies the execution for a block of code. “Else if” statements: this specifies a new test if the first condition is false.

Now that you have the basic JavaScript conditional statement definitions, let’s show you examples of each.

25
Q

What is the syntax for an if/else conditional?

A

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Syntax
condition ? exprIfTrue : exprIfFalse

Parameters
condition
An expression whose value is used as a condition.

exprIfTrue
An expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).

exprIfFalse
An expression which is executed if the condition is falsy (that is, has a value which can be converted to false).

function getFee(isMember) {
  return (isMember ? '$2.00' : '$10.00');
}
console.log(getFee(true));
// expected output: "$2.00"
console.log(getFee(false));
// expected output: "$10.00"
console.log(getFee(null));
// expected output: "$10.00"
26
Q

What is the syntax for an if/else conditional?

What is the syntax for a switch statement?

A

Use the if statement to specify a block of JavaScript code to be executed if a condition is true.

Syntax
if (condition) {
  //  block of code to be executed if the condition is true
}

Example
Make a “Good day” greeting if the hour is less than 18:00:

if (hour < 18) {
greeting = “Good day”;
}

The else if statement specifies a new condition if the first condition is false:

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}
27
Q

What is the syntax for a switch statement?

A
switch(expression) {
  case n:
    code block
    break;
  case n:
    code block
    break;
  default:
    default code block
}

Example

var text;
var fruits = document.getElementById("myInput").value;
switch(fruits) {
  case "Banana":
    text = "Banana is good!";
    break;
  case "Orange":
    text = "I am not a fan of orange.";
    break;
  case "Apple":
    text = "How you like them apples?";
    break;
  default:
    text = "I have never heard of that fruit...";
}
28
Q

What is nesting?

A

Nesting is when you write something inside of something else. You can have a function inside of another function: function x () { function y() { // something; } } You can have an if condition inside of another if condition: if (daylight) { if (before 12) { //It’s morning; } else { // it’s afternoon; } }