IES: JS-deck 5 Flashcards
JS “var”
- JS keyword
- JS significant: avoid in variable/function names
- Used to declare a variable,
- Example
var fruits = ["apple", "banana", "orange"]; var age=22;
JS “void”
- 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
JS “volatile”
- 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.
JS “while”
- 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++;
}
~~~
JS “with”
- 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); }; } }
JS “yield”
- 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
JS Array
- 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" });
JS Date
- 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)
JS eval
- 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’]
JS function
- 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
JS hasOwnProperty
- 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)
JS Infinity
- 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;
JS isFinite
- 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)
JS isNaN
- 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
JS isPrototypeOf
- 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
JS length
- JS Property
- JS significant: avoid for variable/function names
- Determine the size of different data types:
1. Strings: returns the number of characters in.
let message = "Hello, world!"; console.log(message.length); // Output: 13
- Arrays: returns to the number of elements in.
let fruits = ["apple", "banana", "orange"]; console.log(fruits.length); // Output: 3
- Objects: (not directly applicable). However,
Object.keys()
acquires the properties number in an object.
let person = { name: "John", age: 30, city: "New York" }; console.log(Object.keys(person).length); // Output: 3
- Functions: returns the number of parameters expected
function sum(a, b) { return a + b; } console.log(sum.length); // Output: 2
JS Math
- JS Object
- JS significant: avoid for variable/function names
- Has no constructor
- Is static
- Methods & properties can be used without creating this first
- Examples
// Calculating the area of a circle
const radius = 5; const area = Math.PI * Math.pow(radius, 2); console.log("The area of the circle is:", area);
// Generating a random number between 1 and 10
const randomNumber = Math.floor(Math.random() * 10) + 1; console.log("Random number:", randomNumber);
// Finding the square root of a number
const number = 25; const squareRoot = Math.sqrt(number); console.log("The square root of", number, "is:", squareRoot);
JS NaN
- JS Global Property
- JS significant: avoid for variable/function names
- Short for “Not-a-Number”
- Not a legal number
- Global NaN property is the same as the Number. Nan property
- Examples
// Examples of operations that result in NaN
let result1 = 0 / 0; let result2 = Math.sqrt(-1); let result3 = parseInt("Hello");
JS name
- JS Object, Property, or Method
- JS significant: avoid for variable/function names
- Accesses the name of various elements like functions, objects, and HTML elements.
- Examples
1. Function Name
function greet() { console.log("Hello, world!"); } console.log(greet.name); // Output: "greet"
- Object Property
const person = { name: "John Doe", age: 30 }; console.log(person.name); // Output: "John Doe"
- HTML Element Name
<input type="text" name="username">
const usernameInput = document.querySelector("input[name='username']"); console.log(usernameInput.name); // Output: "username"
- Computed Property Name
const propertyName = “city”;
const address = {
[propertyName]: “New York”
};
console.log(address.city); // Output: “New York”
JS Number
- JS Primitive value or Object
- JS significant: avoid for variable/function names
- the Number data type represents both integers and floating-point numbers: JS does not define different types of numbers, like integers, short, long, floating-point etc.
- Always stored as double precision floating point numbers, following international IEEE 754 standard.
- Examples
// Creating number variables
let x = 10; let y = 3.14;
// Performing arithmetic operations
let sum = x + y; let difference = x - y; let product = x * y; let quotient = x / y; let remainder = x % y;
// Using built-in methods
let rounded = Math.round(y); let max = Math.max(x, y); let min = Math.min(x, y); let squareRoot = Math.sqrt(x);
// Converting numbers to stringslet numString = x.toString();
// Checking if a value is a numberlet isNumber = Number.isInteger(x);
console.log("Sum:", sum); console.log("Difference:", difference); console.log("Product:", product); console.log("Quotient:", quotient); console.log("Remainder:", remainder); console.log("Rounded:", rounded); console.log("Max:", max); console.log("Min:", min); console.log("Square Root:", squareRoot); console.log("Number as String:", numString); console.log("Is x a number?", isNumber);