Javascript Basics Flashcards
Name the three ways to declare a variable?
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.
What rules should you follow when naming variables?
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 does the % operator work?
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.
Explain the difference between == and ===.
= 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.
When would you receive a NaN result?
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 do you increment and decrement a number?
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
Explain the difference between prefixing and post-fixing increment/decrement operators.
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).
What are Increment Operators?
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
What are Decrement Operators?
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
What is operator precedence and how is it handled in JS?
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 do you access developer tools and the console?
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 do you log information to the console?
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 .
What are the eight data types in JavaScript?
String. Number. Boolean. Null. Undefined. Symbol. BigInt. Object.
Which data type is NOT primitive?
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’.
What is the relationship between null and undefined?
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