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.