IES: JS-deck 5 Flashcards
1
Q
JS “var”
A
- JS keyword
- JS significant: avoid in variable/function names
- Used to declare a variable,
- Example
~~~
var fruits = [“apple”, “banana”, “orange”];
var age=22;
~~~
2
Q
JS “void”
A
- JS keyword
- JS significant: avoid for variable/function names
- used to evaluates an expression and returns undefined. A void operator is frequently used to get the undefined primitive value.
- Syntax
void expression;
- Example
~~~
void 0; // Returns undefined
void (1 + 2); // Returns undefined
void console.log(“Hello”); // Prints “Hello” to the console, but returns undefined
~~~
3
Q
JS “volatile”
A
- JS keyword
- JS significant: In JavaScript, the
volatile
keyword is not implemented and has been reserved for future use. - In Java, the volatile keyword is a modifier - ensures attribute’s value is the same from all threads: JS is single threaded.
4
Q
JS “while”
A
- JS keyword
- JS significant: avoid in variable/function names
- Used for while loop, while loop executes the block of code until the condition is true.
- Example
~~~
var a=1;
while(a <= 10)
{
document.write(“loop is running for “ + a + “times</p>”);
a++;
}
~~~
5
Q
JS “with”
A
- JS keyword
- JS significant: avoid in variable/function names
- Used for iterating, just, in short, it is shortened for iteration.
- Example
~~~
var fruits = [“apple”, “banana”, “orange”];
for ( var i = fruits.length; i–; ) {
with ({ no : i }) {
link.onclick = function() {
alert(no);
};
}
}
~~~
6
Q
JS “yield”
A
- JS keyword
- JS significant: avoid for variable/function names
- Used to pause and resume a generator function. The generator function is the same as a normal function but for returning a value in place of return it uses yield keyword.
- Example
~~~
function* iter( a ) {
while (a < 4) {
yield a++;
}
}
const i = iter ( 1 );
console.log(i.next().value); //return 1
console.log(i.next().value); //return 2
console.log(i.next().value); //return 3
~~~
7
Q
JS Array
A
- JS object
- JS significant: avoid for variable/function names
- one of most commonly used data types
- An ordered value list identified by an index starting at zero
- Create an array:
array literallet fruits = ["apple", "banana", "orange"];
Array constructorlet numbers = new Array(1, 2, 3, 4, 5);
- Access an array element:
Using its index
~~~
console.log(fruits[0]); // Output: “apple”
console.log(numbers[2]); // Output: 3
~~~ - Modifying array elements:
By assigning a new value to its index
~~~
fruits[1] = “grape”;
console.log(fruits); // Output: [“apple”, “grape”, “orange”]
~~~ - JS built-in methods to manipulate arrays
push()
,pop()
,unshift()
,shift()
,splice()
,concat()
,slice()
,indexOf()
,forEach()
,map()
,filter()
,reduce()
,
Array manipulation Example
~~~
let colors = [“red”, “green”, “blue”];
colors.push(“yellow”); // Add “yellow” to the end
console.log(colors); // Output: [“red”, “green”, “blue”, “yellow”]
colors.pop(); // Remove the last element
console.log(colors); // Output: [“red”, “green”, “blue”]
colors.forEach(function(color) {
console.log(color); // Output: “red”, “green”, “blue”
});
~~~
8
Q
JS Date
A
- JS Object
- JS significant: avoid for variable/function names
- Date objects are static: The computer clock is ticking; date objects are not.
- There are nine ways to create a new date object:
1. new Date()
2. new Date(date string)
3. new Date(year,month)
4. new Date(year,month,day)
5. new Date(year,month,day,hours)
6. new Date(year,month,day,hours,minutes)
7. new Date(year,month,day,hours,minutes,seconds)
8. new Date(year,month,day,hours,minutes,seconds,ms)
9. new Date(milliseconds)
9
Q
JS eval
A
- JS Method
- JS significant: avoid for variable/function names
- evaluates or executes its argument.
- If the argument is an expression, it evaluates the expression.
- If it’s one or more statements, it executes the statements.
- Syntax
eval(string);
- Example
function func() {
// Original string
let a = 2;
let b = 2;
// Finding the sum
let value = eval(new String(a + b));
console.log(value);
} - Output
[String: ‘4’]
10
Q
JS function
A
- JS Object, Property, or Method
- JS significant: avoid for variable/function names
- block of code designed to perform a specific task. It’s a reusable piece of code that can be called multiple times
- Examples
Simple
~~~
function greet(name) {
console.log(“Hello, “ + name + “!”);
}
greet(“John”); // Output: Hello, John!
~~~
With return value
~~~
function add(x, y) {
return x + y;
}
let sum = add(5, 3);
console.log(sum); // Output: 8
~~~
Anonymous function
~~~
const square = function(x) {
return x * x;
}
console.log(square(4)); // Output: 16
~~~
Arrow function (ES6 syntax)
~~~
const multiply = (x, y) => x * y;
console.log(multiply(2, 6)); // Output: 12
~~~
11
Q
JS hasOwnProperty
A
- JS Method
- JS significant: avoid for variable/function names
- used to check if an object has a specific property as its own property (not inherited from its prototype chain).
- Example
~~~
const person = {
name: “John”,
age: 30
};
console.log(person.hasOwnProperty(“name”)); // true
console.log(person.hasOwnProperty(“age”)); // true
console.log(person.hasOwnProperty(“toString”)); // false (inherited from Object.prototype)
~~~
12
Q
JS Infinity
A
- JS “Global” Method
- JS significant: avoid for variable/function names
- Infinity is a number that represents positive infinity.
A number reaches Infinity when it exceeds the upper limit for a number: (1.797693134862315E+308.) - Example
~~~
let x = 1.797693134862315E+308;
let y = x * 1.001;
~~~ - -Infinity represents negative infinity.
A number reaches -Infinity when it exceeds the lower limit for a number: (-1.797693134862316E+308.) - Example
~~~
let x = -1.797693134862316E+308;
let y = x * 1.001;
~~~
13
Q
JS isFinite
A
- JS Method
- JS significant: avoid for variable/function names
- determines whether the passed value is a finite number.
- Returns false if positive infinity, negative infinity, or NaN
- Syntax
Number.isFinite(value);
- Example
Number.isFinite(123); // true
Number.isFinite(-1.23); // true
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite(NaN); // false
Number.isFinite(“123”); // false (because it’s a string, not a number)
14
Q
JS isNaN
A
- JS Method
- JS significant: avoid for variable/function names
- Determines whether a value is NaN( “Not a Number” ): Return or output is either “true” or “false”
- Important
1.isNaN()
first attempts to convert the given value to a number. If the conversion results in NaN, it returns true. This can lead to unexpected results.
2.Number.isNaN()
is a more precise alternative that doesn’t perform type coercion. It only returns true if the value is exactly the NaN value. - Examples
~~~
isNaN(NaN); // true
isNaN(Number.NaN); // true
isNaN(0 / 0); // true
~~~
isNaN(123); // false isNaN("123"); // false, because "123" can be converted to a number isNaN("Hello"); // true, because "Hello" cannot be converted to a number isNaN(true); // false, because true is converted to 1 isNaN(false); // false, because false is converted to 0
15
Q
JS isPrototypeOf
A
- JS Method
- JS significant: avoid for variable/function names
- Determines if an object is in the prototype chain of another object
- Definition
Object.prototype.isPrototypeOf(object)
- Examples new
~~~
function Person(name) {
this.name = name;
}
~~~const person1 = new Person("Alice"); console.log(Person.prototype.isPrototypeOf(person1)); // true console.log(Object.prototype.isPrototypeOf(person1)); // true ~~~
const obj1 = { a: 1 };
const obj2 = Object.create(obj1);
~~~console.log(obj1.isPrototypeOf(obj2)); // true