What is JavaScript? Flashcards

1
Q

What is JavaScript?

A

JavaScript is a lightweight scripting language that allows you to implement complex features on web pages. Alongside HTML and CSS, JavaScript is one of the core technologies of the World Wide Web. JavaScript enables interactive web pages and is an essential part of web applications. The vast majority of websites use it for client-side page behavior, and all major web browsers have a dedicated JavaScript engine to execute it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a scripting language?

A

Scripting languages are programming languages that do not require the compilation step and are rather interpreted. For example, normally, a C program needs to be compiled before running whereas normally, a scripting language like JavaScript or PHP need not be compiled. Another point to be noted is that while classifying a language as scripting language or programming language, the environment on which it would execute must be taken into consideration. The reason why this is important is that we can design an interpreter for C language and use it as a scripting language, and at the same time, we can design a compiler for JavaScript and use it as a non-scripting(compiled language).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two categories of APIs?

A
  1. Browser APIs

2. Third party APIs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are browser APIs?

A
Browser APIs are built into your web browser, and are able to expose data from the surrounding computer environment, or do useful complex things. 
For example:
The DOM (Document Object Model) API allows you to manipulate HTML and CSS, creating, removing and changing HTML, dynamically applying new styles to your page, etc. Every time you see a popup window appear on a page, or some new content displayed that's the DOM in action.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are Third party APIs?

A

Third party APIs are not built into the browser by default, and you generally have to grab their code and information from somewhere on the Web. For example:
For example:
The Twitter API allows you to do things like displaying your latest tweets on your website.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is JavaScript doing on your page?

A

When you load a web page in your browser, you are running your code (the HTML, CSS, and JavaScript) inside an execution environment (the browser tab). This is like a factory that takes in raw materials (the code) and outputs a product (the web page).A very common use of JavaScript is to dynamically modify HTML and CSS to update a user interface, via the Document Object Model API (as mentioned above).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain Just in Time Compiling

A

In computing, just-in-time (JIT) compilation (also dynamic translation or run-time compilations)[1] is a way of executing computer code that involves compilation during execution of a program – at run time – rather than before execution.[2] Most often, this consists of source code or more commonly bytecode translation to machine code, which is then executed directly. A system implementing a JIT compiler typically continuously analyses the code being executed and identifies parts of the code where the speedup gained from compilation or recompilation would outweigh the overhead of compiling that code.

JIT compilation is a combination of the two traditional approaches to translation to machine code – ahead-of-time compilation (AOT), and interpretation – and combines some advantages and drawbacks of both.[2] Roughly, JIT compilation combines the speed of compiled code with the flexibility of interpretation, with the overhead of an interpreter and the additional overhead of compiling and linking (not just interpreting)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

async vs defer

A

(only in external js) async and defer both instruct the browser to download the script(s) in a separate thread, while the rest of the page (the DOM, etc.) is downloading, so the page loading is not blocked by the scripts.
If your scripts should be run immediately and they don’t have any dependencies, then use async.
If your scripts need to wait for parsing and depend on other scripts and/or the DOM being in place, load them using defer and put their corresponding elements in the order you want the browser to execute them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Types of error

A
  1. Syntax errors

2. Logical errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

JS running order

A

The code in your web documents is generally loaded and executed in the order it appears on the page. If the JavaScript loads and tries to run before the HTML and CSS it is affecting has been loaded, errors can occur.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Browser security

A

Each browser tab has its own separate bucket for running code in (these buckets are called “execution environments” in technical terms) — this means that in most cases the code in each tab is run completely separately, and the code in one tab cannot directly affect the code in another tab — or on another website. This is a good security measure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Just-in-time compiling in JS

A

From a technical standpoint, most modern JavaScript interpreters actually use a technique called just-in-time compiling to improve performance; the JavaScript source code gets compiled into a faster, binary format while the script is being used, so that it can be run as quickly as possible. However, JavaScript is still considered an interpreted language, since the compilation is handled at run time, rather than ahead of time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Client-side code

A

Client-side code is code that is run on the user’s computer — when a web page is viewed, the page’s client-side code is downloaded, then run and displayed by the browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Server-side code

A

Server-side code on the other hand is run on the server, then its results are downloaded and displayed in the browser. Examples of popular server-side web languages include PHP, Python, Ruby, ASP.NET and… JavaScript! JavaScript can also be used as a server-side language, for example in the popular Node.js environment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Dynamic versus static code

A

The word dynamic is used to describe both client-side JavaScript, and server-side languages — it refers to the ability to update the display of a web page/app to show different things in different circumstances, generating new content as required. Server-side code dynamically generates new content on the server, e.g. pulling data from a database, whereas client-side JavaScript dynamically generates new content inside the browser on the client, e.g. creating a new HTML table, filling it with data requested from the server, then displaying the table in a web page shown to the user. The meaning is slightly different in the two contexts, but related, and both approaches (server-side and client-side) usually work together.

A web page with no dynamically updating content is referred to as static — it just shows the same content all the tim

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Script loading strategies

A
  1. In the internal JS, you can see this structure around the code:

document.addEventListener(“DOMContentLoaded”, function() {

});

This is an event listener, which listens for the browser’s “DOMContentLoaded” event, which signifies that the HTML body is completely loaded and parsed. The JavaScript inside this block will not run until after that event is fired, therefore the error is avoided

  1. In the external JS, we use a more modern JavaScript feature to solve the problem, the defer attribute, which tells the browser to continue downloading the HTML content once the tag element has been reached.

In this case both the script and the HTML will load simultaneously and the code will work.

  1. An old-fashioned solution to this problem used to be to put your script element right at the bottom of the body (e.g. just before the
17
Q

async vs defer

A
  1. async and defer both instruct the browser to download the script(s) in a separate thread, while the rest of the page (the DOM, etc.) is downloading, so the page loading is not blocked by the scripts.
  2. If your scripts should be run immediately and they don’t have any dependencies, then use async.
  3. If your scripts need to wait for parsing and depend on other scripts and/or the DOM being in place, load them using defer and put their corresponding elements in the order you want the browser to execute them.
18
Q

Events inJS

A

Events are things that happen in the browser — a button being clicked, a page loading, a video playing, etc. — in response to which we can run blocks of code. The constructs that listen out for the event happening are called event listeners, and the blocks of code that run in response to the event firing are called event handlers.

19
Q

Diffference between var and let

A
  1. If you write a multiline JavaScript program that declares and initializes a variable, you can actually declare a variable with var after you initialize it and it will still work. This works because of hoisting
  2. Secondly, when you use var, you can declare the same variable as many times as you like, but with let you can’t.
20
Q

Variable types in JS

A
  1. Numbers (i. Integers ii. Decimal numbers)
  2. Strings
  3. Booleans
  4. Arrays
  5. Objects
21
Q

Dynamically typed language

A

JavaScript is a “dynamically typed language”, which means that, unlike some other languages, you don’t need to specify what data type a variable will contain (numbers, strings, arrays, etc).

22
Q

Useful Number methods

A
  1. toFixed() - to round your number to a fixed number of decimal places
  2. Number() - passing the string value into the Number() constructor to return a number version of the same value.
23
Q

Template literals

A

To turn a standard string literal into a template literal, you have to replace the quote marks (‘ ‘, or “ “) with backtick characters ( ). When you want to include a variable or expression inside the string, you include it inside a ${ } construct, which is called a placeholder.

24
Q

Useful String methods

A
  1. toString()
  2. length property - eg. string1.length;
  3. indexOf() - If the substring is found inside the main string, it returns a number representing the index position of the substring — which character number of the main string the substring starts at. If the substring is not found inside the main string, it returns a value of -1
  4. slice() - When you know where a substring starts inside a string, and you know at which character you want it to end, slice() can be used to extract it. So the slice happens from the first position, up to, but not including, the last position.
  5. toLowerCase() and toUpperCase()
  6. replace() - You can replace one substring inside a string with another substring using the replace() method.
25
Q

Useful array methods

A
  1. split() - , this takes a single parameter, the character you want to separate the string at, and returns the substrings between the separator as items in an array.
  2. join() - You can also go the opposite way using the join() method. Converts an array to a string. let myNewString = myArray.join(‘,’);
  3. toString() - is arguably simpler than join() as it doesn’t take a parameter, but more limiting. With join() you can specify different separators, whereas toString() always uses a comma.
  4. push() & pop() - add or remove an item at the end of an array
  5. unshift() and shift() - add or remove an item at the beginning of an array