Week 5 Flashcards
What are the datatypes in JavaScript?
Number, bigInt (values larger than -253 or 253), boolean, string, undefined, null, array (stores list of variables under a single variable), and object
What is Javascript?
Javascript is the most commonly used client-side language. It is a high-level, multi-paradigm, interpreted programming language used to create dynamic webpages. The browser interprets Javascript code in it and executes it.
Can JavaScript code be run on servers as the backend program for an application?
Although JavaScript originated as a scripting language that runs in the browser, the Node.js runtime environment does allow JavaScript code to be run on servers as the backend program for an application.
What does it mean to say JavaScript is a high level language?
It abstracts away many implementation details that relate to computer hardware - like allocating memory or garbage collection of objects.
What does it mean to say JavaScript is multi-paradigm?
It supports many programming paradigms like procedural, object-oriented, and functional programming.
What is the official name for JavaScript?
ECMAScript
In HTML, where is JavaScript code written?
In HTML, JavaScript code is written inside the and tags. You can place any number of scripts in an HTML document. Scripts can be placed in the , or in the section of an HTML page, or in both.
What is “internal JavaScript”?
When the JavaScript is code placed anywhere within the HTML page using tags
What is “external JavaScript”?
When JavaScript code is placed in a separate file from the HTML code. These files are saved with the “. js” extension and imported into the HTML page using the src attribute. The src attribute specifies the URL/path of an external JavaScript file. (Eg: )
Is JavaScript case sensitive?
Yes
How are statements separated in JavaScript?
Every statement in JavaScript is separated using a semicolon (;). JavaScript ignores multiple spaces and tabs.
What are JavaScript Literals?
Literals are the fixed values, they can be numbers, strings, boolean values, etc. The number type stores integer, float, and hexadecimal value. Strings are text, enclosed within single or double quotes (‘ or “). If numbers, characters, or words are enclosed with single or double quotes, then they are considered strings.
What are JavaScript keywords?
Keywords are tokens that have special meaning in JavaScript. The keywords supported by JavaScript are break, case, catch, continue, do, else, finally, for, function, if, in, new, return, switch, this, throw, try, typeof, var, void, while, etc.
What are JavaScript variables and how are they declared?
Variables are used to store data values. It uses the var keyword to declare variables. An equal sign (=) is used to assign values to variables. Variable names are identifiers that should not be a JavaScript keyword. They start only with the alphabet, underscores (_) or dollar ($). They cannot start with a number and also there shouldn’t be spaces in-between.
What are the Arithmetic Operators in JavaScript and what do they mean?
\+ addition - subtraction * multiplication / division % calculates remainder in a quotient \++ increment -- decrement
What are the Comparison Operators and what do they mean?
== “equals” - checks value NOT variable type - returns true or false boolean
=== “equals” - checks value AND variable type - returns true or false boolean
!= “not equal”
> greater than
< less than
>= greater than or equal to
<= less than or equal to
What are the Logical Operators and what do they mean?
&& and
|| or
! not
What are the Assignment Operators and what do they mean?
= is equal to \+= increment and is equal to -= decrement and is equal to *= multiply and is equal to /= divide and is equal to %= divides and assigns remainder to the variable
What is the Ternary Operator and what does it mean?
< condition > ? < value1 > : < value2 >;
Javascript operator that takes 3 operands. First a condition followed by a ?, then an expression to execute if the condition is true followed by a :, and finally an expression to execute if the condition is false. Frequently used as an alternative to an if…else statement
Ex. 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"
What are the Control Flow statements in JavaScript?
if/else for for-in for-of while do-while
What is the difference between for-in and for-of in JavaScript?
for-in allows iteration over the keys of an object
for-of allows iteration over an array or array-like object
Ex. let person = { name: 'Bob', age: 25 };
for (let prop in person) {
console.log(person[prop]); // prints ‘Bob’ and then 25
}
let people = [ { name: 'Alice', age: 30 }, { name: 'Charlie', age: 29 } ]
for (let pers of people) {
console.log(pers.name); // prints ‘Alice’ then ‘Charlie’
}
What are the advantages and disadvantages of JavaScript as compared to other programming languages?
Advantages
- Is a dynamically-typed language. JavaScript does not declare types before declaring or assigning a variable. Variables can hold the value of any data type at any point in time.
- JavaScript can run on a server or on a browser. Java and C# would have to be compiled to WebAssembly in order to run on modern-day browsers.
- JavaScript is a multi-paradigm language, meaning we can solve our problems in a variety of ways, functionally, object-oriented, or event-driven. With Java and C#, while many of those paradigms have support in the language, Object-oriented solutions are preferred. In JavaScript, functions are first-class variables, meaning that they are treated like any other variable and can be passed as arguments to other functions.
- JavaScript utilizes prototype-based objects. Java and C# utilize class-based objects. In ES6, JavaScript introduced class-based syntax which allows us to interface with our prototypes in a similar manner to Java and C#. However, OOP in JavaScript is accomplished very differently than in these other languages.
Disadvantages
- Due to being a dynamically-typed language, there is no static type checking available which could lead to issues with functions return a value of multiple types
- JavaScript is single-threaded and runs off of an event-loop. Java and C# support multi-threading.
- Due to using prototypes, an “overridden” method is merely shadowed on the prototype and is therefore still accessible.
- JavaScript has no concept of method/function overloading.
- Encapsulation is possible in JavaScript but there are no access modifiers and is not as simple as in other languages.
What is a function in JavaScript?
A group of reusable code which can be called anywhere in the program. All functions are passed by value meaning the value of any variable passed to a function is copied into the argument of the function. Any changes you make to the argument will not be reflected in the variable outside of the function.
What is an anonymous function in JavaScript?
An anonymous function is a function that is declared without any identifier to refer to it. It is an expression that the variable holds a function.
Ex.
var x = function (a, b) {return a * b};
Ex.
var anon = function() { alert('I am anonymous'); }; var prd = function (a, b) { return a * b;
};
anon();
alert(“prd = “ + prd(2,4));