Javascript | Basics Flashcards
Debugging in Ruby happens with ‘pry’ – how about with JS?
In Javascript debugging happens with ‘debugger’
How is a function / method defined in JS as compared to Ruby?
In Ruby:
def foo(a, b)
a + b
end
In Javascript we’ll typically want to store a function inside a variable:
let fn = function foo(a,b){ return a + b; }
What do these functions do?
parseInt(“123”)
parseFloat(“123.45”)
Similar to .to_i method in Ruby, these functions parse a string and return an integer.
What does the string.match() function do?
The string.match() is an inbuilt function in JavaScript which is used to search a string for a match against a any regular expression and if the match is found then it will return the match as an array.
For example:
Input:
let string = "Welcome to GEEKS for geeks!"; string.match(/eek/gi);
Output:
EEK, eek
In the above example substring “eek” will match with given string and it will return instantly if it found the match. Here “gi” parameter helps to find the case-sensitive match and all possible combination in the given string.
What does the string.replace(a, b) function do?
The string.replace() is an inbuilt function in JavaScript which is used to replace a part of the given string with some another string or a regular expression. The original string will remain unchanged.
Here the parameter A is regular expression and B is a string which will replace the content of the given string.
For example:
// Assigning a string let string = 'GeeksForGeeks is a CS portal';
// Calling replace() function let newstring = string.replace(/GeeksForGeeks/, 'gfg');
// Printing replaced string console.log(newstring);
Output: gfg is a CS portal
Explain how the 3 Javascript dialog boxes are used?
JavaScript uses 3 kind of dialog boxes : ALERT, PROMPT and CONFIRM. These dialog boxes can be of very much help for making our website look more attractive.
ALERT BOX :
An alert box is used in the website to show a warning message to the user. Alert box gives only one button “OK” to select and proceed.
Example 1:
if (string == "Hello"){ alert("Hi"); } else{ alert("something is wrong!"); }
Example:
function Warning() {
alert (“Warning danger you have not filled everything”);
console.log (“Warning danger you have not filled everything”);
}
CONFIRM BOX :
A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” or “Cancel” to proceed. If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false and will show null.
PROMPT BOX :
A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks the OK button, the window method prompt() will return the entered value from the text box. If the user clicks the Cancel button, the window method prompt() returns null.
Source: https://www.geeksforgeeks.org/javascript-dialogue-boxes/
In JS, what is the equivalent of Ruby’s “case..when” statement and how does it work?
A “switch..when” statement.
switch (name){ case "Thomas": alert("Hi Thomas!") break case "John": alert("Hi John.") break default: alert("Who are you?")
Ruby has its iterators. The equivalent in JS is “loops”. Give an example of how each is written:
switch statement
while statement
for statement
for statement with ‘let…in’
// switch statement
switch (name){ case "Thomas": alert("Hi Thomas!") break case "John": alert("Hi John.") break default: alert("Who are you?") }
// while statement
while (i <= n){
console.log(i);
i++;
}
// do something until a value (n) is reached
// don’t forget to have i++ or you will loop forever
// for statement
for (let i=0; i<=n; i++){ console.log(i); } // another way to loop an n number of times
// for statement with ‘let…in’
for (let key in person){
console.log(key)
}
// do something with person[key]
What is a javascript prototype? How do you add a new property or method/function to it?
Prototype Inheritance
All JavaScript objects inherit properties and methods from a prototype.
Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Person objects inherit from Person.prototype.
The Object.prototype is on the top of the prototype inheritance chain: Date objects, Array objects, and Person objects inherit from Object.prototype.
To add a new PROPERTY to e.g. the Person property:
the JavaScript prototype property allows you to add new properties to object constructors:
Example function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }
Person.prototype.nationality = “English”;
The JavaScript prototype property also allows you to add new METHODS to objects constructors:
Example function Person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }
Person.prototype.name = function() {
return this.firstName + “ “ + this.lastName;
};
Source: https://www.w3schools.com/js/js_object_prototypes.asp
As compared to Ruby, Javascript didn’t have any ‘classes’ until ES6. They had functions with ‘constructors’. But now with ES6, there are classes in JS as well (technically, they’re actually just syntactic sugar over object prototypes).
Give and example of how to create classes in JS.
And how do you create an object (i.e. ‘instance’) from that ‘class’?
Not, you can add methods to it post-creation.
Example:
// Classes
class Person { // in ruby this is called? initialize // create "constructor" constructor(name, gender, age) { this.name = name this.gender = gender this.age = age // public variables -- 'this' references current object } greet() { // public function console.log(`Hello, I am ${this.name}`) } }
john = new Person('John', 'male', 20) john.greet()
output: Hello, I am John
You then construct an object (an instance) like this:
let bob = new Person("Anna", "Andersson"); // "new" creates an object
What is ‘this’ and what does it refer to?
‘this’ refers to the object that called the method / property.
What does the setTimeout() function do? E.g:
timer = setTimeout(“myfunction()”, 1000)
setTimeout is a native JavaScript function which calls a function or executes a code snippet after a specified delay (in milliseconds)
// execute in 1 second (1000ms)
What does the eval() function do?
The eval() function evaluates or executes an argument.
eval() is a function property of the global object.
The argument of the eval() function is a string. If the string represents an expression, eval() evaluates the expression. If the argument represents one or more JavaScript statements, eval() evaluates the statements. Do not call eval() to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
Examples:
eval(new String('2 + 2')); // returns a String object containing "2 + 2"
eval('2 + 2'); // returns 4
What does the ‘instanceof’ operator do?
Class checking: “instanceof”
The instanceof operator allows to check whether an object belongs to a certain class and returns a boolean value (true/false). It also takes inheritance into account.
The syntax is:
obj instanceof Class
It returns true if obj belongs to the Class (or a class inheriting from it).
For instance:
alert( arr instanceof Array ); // true
alert( arr instanceof Object ); // true
class Rabbit {} let rabbit = new Rabbit();
// is it an object of Rabbit class? alert( rabbit instanceof Rabbit ); // true