JavaScript Language Fundamentals Flashcards
What is JavaScript? What do we use it for?
JavaScript is a scripting language that allows you to implement complex features on web pages
Can we run JavaScript in a web browser, on a server, or both?
The power of JavaScript is not only limited to the front-end browsers. It can also be used on the server, or both at the same time. This can be done using Node.js.
In most cases, the usage of Node.js is within the back-end web servers that exposes APIs to the front-end.
What programming paradigm(s) does JS support?
JavaScript supports both object-oriented programming with prototypal inheritance as well as functional and imperative programming.
What are the data types in JS?
There are 8 basic data types in JavaScript.
-number for numbers of any kind:
integer or floating-point, integers are limited by ±(253-1).
-bigint is for integer numbers of arbitrary length.
-strings. A string may have zero or more characters, there’s no separate single-character type.
-boolean for true/false.
-null for unknown values - a standalone type that has a single value null.
-undefined for unassigned values - a standalone type that has a single value undefined.
-object and symbols for more complex data structures.
What is the type of NaN? What is the isNaN function?
The isNaN() function determines whether a value is an illegal number (Not-a-Number).
This function returns true if the value equates to NaN. Otherwise it returns false.
NaN is a property of the global object. In other words, it is a variable in global scope.
The initial value of NaN is Not-A-Number — the same as the value of Number. In modern browsers, NaN is a non-configurable, non-writable pro
What is the data type of a function?
The data type of a function is object
What about an array?
The JavaScript Array class is a global object that is used in the construction of arrays; which are high-level, list-like objects. Type is object
What is the difference between undefined and null?
Null is used to represent an intentional absence of value. It represents a variable whose value is undefined. It accepts only one value, which is null. The Null keyword is used to define the Null type in TypeScript, but it is not useful because we can only assign a null value to it.
Undefined represents uninitialized variables in TypeScript and JavaScript. It has only one value, which is undefined.
What are JS objects? what is the syntax?
A JavaScript object is an entity having state and behavior (properties and method).
There are 3 ways to create objects:
-By object literal
-By creating instance of Object directly (using new keyword)
-By using an object constructor (using new keyword)
What is JSON? Is it different from JS objects?
The syntax of JSON was inspired by the JavaScript Object Literal notation, but there are differences between them.
JSON stands for “JavaScript Object Notation”. It’s a string representation of a JavaScript object. It always has the type string.
You can turn a JavaScript object into a string with JSON.stringify
The quotes are mandatory on JSON
What are some ways you can use functions in JS?
The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
There are also expression functions. A function expression is very similar in syntax to a function declaration. The major difference is that a function expression does not need a function name. Function expressions are a part of another statement.
Arrow functions are an ES6 addition and are meant to be a syntactically compact alternative to function expressions. Arrow functions are defined using a pair of parentheses containing a list of parameters, followed by a fat arrow => and then the function statements with curly braces {}.
Anonymous functions are always loaded using a variable name. Anonymous, as the name suggests, allows creating a function without any names identifier. It can be used as an argument to other functions.
What are the different scopes of variables in JS?
Let
Let makes use of block scope. If you declare a variable with the let keyword, it will be accessible inside of a current block scope created with a block statement.
Const
Const behaves the same as let when it comes to scopes. It is, as the name suggests, a constant.
Var
Before ES6 the only type of variable declaration keyword we had was var. Variables declared with var are function-scoped. It means, that declaring a variable with var makes it accessible throughout the whole function.
Global variables declared with the var keyword are attached to the window object (or the global object, if you are using Node.js). Even if you declare variables at the bottom of the function, they are going to be hoisted to the top (it does not happen with let and const).
What are the different ways to declare global variables?
Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code using the var keyword or window global object
The proper way is to use window object. And use the syntax like the following example:
var window.iAmGlobal = “some val”; //Global variable declaration with window
Is it a best practice to use global variables? Why or why not?
Having many global variables is always a bad thing because it’s easy to forget you declared a variable somewhere and accidentally re-declare it somewhere else. If your first variable was local then you don’t have a problem.
What is function and variable hoisting?
Variable hoisting is not the same as function hoisting- it is unique in ways of its own. Remember there are 2 ways of creating functions- Function Declaration and Function Expression.
In Javascript, function declarations hoist the function definitions. This means that these functions can be used even before they are declared.
Unlike variables, a function declaration doesn’t just hoist the function’s name. It also hoists the actual function definition.
What is the global object in client-side JavaScript?
The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment.
What are some built-in functions (methods on the global object)?
alert() Displays an alert box with a message and an OK button
close() Closes the current window
open()Opens a new browser window
prompt()Displays a dialog box that prompts the visitor for input
How would you set some code to run at a specified time in the future? What about repeatedly?
JavaScript Callback function are the most special and important function of JavaScript whose main aim is to pass another function as a parameter where the callback function runs which means one function when infused into another function with the parameters is again called as per the requirement. The parameters passed by the function at the time of invocation must be kept in mind by passing otherwise it might create compilation error at the time of calling the function.
Explain how the guard and default operators work
&& operators on a return value provides us a guarding clause - IE return loggedIn && username would return username only if ‘loggedin’ is true|| default operator gives us a way to say return ‘something’ and if its not here return some default value ie return username || “user not found”
What are callback functions? What about self-invoking functions?
Callbacks are functions that you pass into another function to be invoked at a later time. Self invoking functions are wrapped around parentheses and another set of parentheses is adjacent to it.
Example: (function() { console.log(“Hey”)})();
What is closure and when should you use it?
A closure is a function having access to the parent scope, even after the parent function has closed.The variable add is assigned to the return value of a self-invoking function. The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression. This way add becomes a function.
It can access the counter in the parent scope. This is called a JavaScript closure. It makes it possible for a function to have “private” variables. The counter is protected by the scope of the anonymous function, and can only be changed using the add function.
Use the object literal syntax to create an object with some properties
let wezleysobject = { name: “wezley”, occupation: “teacher”, level: “9001”}
What is a truthy or falsy value? List the falsy values.
Falsy values are evaulated to false in certain contexts. The falsy values are: 0, null, undefined, false, “”, and NaN.
Everything else is defined as truthy.
What is the difference between == and ===? Which one allows for type coercion?
The === operator is strict equality evauation. == will attempt to do type conversion.