JavaScript Flashcards

1
Q

What is ES6?

A

ES6 refers to version 6 of the ECMA Script programming language. … It is a major enhancement to the JavaScript language, and adds many more features intended to make large-scale software development easier. ECMAScript, or ES6, was published in June 2015. It was subsequently renamed to ECMAScript 2015

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

What is ECMA Script?

A

ECMAScript is a JavaScript standard meant to ensure the interoperability of web pages across different web browsers. It is standardised by Ecma International according to the document ECMA-262

European Computer Manufacturers Association Script - It is an organisation that create standards for technology

ECMA-262 is a standard published by Ecma International. It contains the specification for a general purpose scripting language.

Think of ECMA-262 as ECMAScript’s reference number.

There are many other ECMA standards

The specification defined in ECMA-262 for creating a general purpose scripting language.
Synonym: ECMAScript specification

While ECMA-262 is the name of the standard, it represents the scripting language specification ECMAScript.

ECMAScript provides the rules, details, and guidelines that a scripting language must observe to be considered ECMAScript compliant.

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

What is a JavaScript engine and examples?

A

A program or interpreter that understands and executes JavaScript code.

Synonyms: JavaScript interpreter, JavaScript implementation

JavaScript engines are commonly found in web browsers, including V8 in Chrome, SpiderMonkey in Firefox, and Chakra in Edge. Each engine is like a language module for its application, allowing it to support a certain subset of the JavaScript language.

A JavaScript engine to a browser is like language comprehension to a person. If we re-visit our example of the actions “walk”, “run”, “jump”, a JavaScript engine is the part of an “entity” that actually understands what these actions mean.

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

What is significant about Javascript support in browsers and ECMA Script?

A

With regards to browser support, people usually talk about “ECMAScript compatibility” rather than “JavaScript compatibility,” even though JavaScript engines parse and execute… well, JavaScript. This can be a little confusing, but there is an explanation.

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

What is a Javascript runtime and how does it differ from a javascript engine?

A

In short, the environment is where the code is running, the engine is what inteprets the code.

Runtime

  • Runtime is the environment in which the JavaScript code runs and is interpreted by a JavaScript engine.
  • The runtime provides the host objects that JavaScript can operate on and work with.
  • Synonyms: Host environment
  • For the server side, the JavaScript runtime is Node.js.
  • Server-related host objects such as the file system, processes, and requests are provided in Node.js. Otherwise the runtime is a browser.
  • An interesting point: different JavaScript runtimes can share the same JavaScript engine. V8, for example, is the JavaScript engine used in both Google Chrome and Node.js — two very different environments.
  • Server-related host objects are for example window.alert(). This is a method from the browser. We’re executing code in the browser’s rungime environment.
  • When you are running in Node environment you use console.log instead of window.alert because the window object is not available.

Javascript Engine:

  • Translate source code into machine code that allows a computer to perform specific tasks.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are let based variables in Javascript?

A
  • The let keyword allows you to declare variables that are limited to the scope of a block statement or expression on which it is used, unlike the var keyword which declares a variable globally or locally to an entire function regardless of block scope1.
  • Variables declared with let are only usable in lines of code that occur after the variable is declared.
  • let variables may not be redeclared by a subsequent var or let.
  • Global let variables are not added to the global window objectlet variables are easy to use with closures (they do not cause race conditions)2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are constant variables in Javascript? Write the syntax

A

With ES6 you not able to reassign or redeclare a constant variable.

const varname = ‘test’;

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

What is the variable scope difference between var, const and let?

A

var, let and const is limited to function scope. Cant access outside function.

Let and const are block level scoped, but not var. Block level means within a thing like statements like If etc. Block level is accessible within a set of curly braces.

If you want to access let or const outside a block you need to declare it at a higher level.

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

What is an arrow function in ES6?
When should you use them?
Type the ES5 vs ES6 version.

A

* Arrow functions first introduced in ES6 (ECMA 2015 is the most recent)
* ES5 Does not have Arrow Functions.
* First of all, ArrowArrow functions make functions shorter by taking out the keyword function.
* Important to know that es6 arrow functions are nameless and cant be referenced again.
* If you want to reference the function, you need to assign it to a variable like in the example below.
* In the concise version you dont need a body or return statement. You can write short functions much easier.

They were added:
* Reduces lots of code and makes the mode more readable.
* Arrow function syntax automatically binds “this” to the surrounding code’s context.
* Writing the arrow => is more flexible as compared with the writing function keyword.

So for example in ES5 it was:

function square (x){
return x;
}
Verbose es6:
const squareArrow = (x) =\> {
return x;
}

Consise es6:
const SquareArrow = (x) => x*x;
(Assumed there is a return statement)
console.log(squareArrow(9));

Without Parenthesis
hello = val => “Hello “ + val;

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

What does the “this” keyword refer to?

A
  • In a method, this refers to the owner object.
  • Alone, this refers to the global object.
  • In a function, this refers to the global object.
  • In a function, in strict mode, this is undefined.
  • In an event, this refers to the element that received the event.
  • Methods like call(), and apply() can refer this to any object.
  • When used alone, the owner is the Global object, so this refers to the Global object.
  • In a browser window the Global object is [object Window]
  • In strict mode, when used alone, this also refers to the Global object [object Window]:
 fullName : function() { 
 return this.firstName + " " + this.lastName; 
} 
So, in a function, this refers to the Global object [object Window]. 
function myFunction() { 
 return this; 
} 

JavaScript strict mode does not allow default binding.

So, when used in a function, in strict mode, this is undefined.

"use strict"; 
function myFunction() { 
 return this; 
} 

In HTML event handlers, this refers to the HTML element that received the event:

Click to Remove Me!

The call() and apply() methods are predefined JavaScript methods.

They can both be used to call an object method with another object as argument.

In the example below, when calling person1.fullName with person2 as argument, this will refer to person2, even if it is a method of person1:

var person1 = { 
 fullName: function() { 
 return this.firstName + " " + this.lastName; 
 } 
} 
var person2 = { 
 firstName:"John", 
 lastName: "Doe", 
} 
person1.fullName.call(person2); // Will return "John Doe"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the characteristics and differences between Functions and Methods in JS?

A

JavaScript Functions:

  • A block of code written to perform some specific set of tasks.
  • Defined using the function keyword, followed by Name and optional parameters. Body of function is enclosed in Curly braces

Features:

  • The function is executed when something calls/invokes it.
  • The name may contain letters, digits, dollar signs, underscore.
  • Parameters are listed inside round parenthesis after the name of the function.
  • Arguments are values a function receives when it is invoked.
  • When the control reaches the return statement, js will stop executing and the value is returned to the caller.

Method

  • Is a property of an object that contains a function definition.
  • Methods are functions stored as object properties

Features:

  • Actions that can be performed on objects are what we term JavaScript methods.
  • The objects can also be called without using parenthesis.
  • This refers to the owner object in a method.

Differences

  • A function can be called directly by its name. A method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation..
  • Data passed to a function is explicit. A method implicitly passes the object on which it was called.
  • A function lives on its own. A method is a function associated with an object property.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the javascript Map method and how do you write it?

A

Map created a new array from an existing array and populates the it with the results.

This example uses an arrow function.

Const testmessage = this.cities.map ((city) => {
return this.name +’ message ‘+ city;
};
return testmessage;

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

What is the relationship between Javascript and ECMAScript

A

JavaScript is a general-purpose scripting language that conforms to the ECMAScript specification. The ECMAScript specification is a blueprint for creating a scripting language. JavaScript is an implementation of that blueprint. On the whole, JavaScript implements the ECMAScript specification as described in ECMA-262.

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

Why is it called JAVAscript?

A

Initially it was called LiveScript, but it was decided that positioning it as a younger brother of Java would be better. It later became more independent with its own specification called ECMAScript.

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

What are some examples of Javascript engines?

A

Javascript engines are also sometimes called a JavaScript Virtual Machine.
V8 - chrome, opera, edge,
Spidermonkey - Firefix

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

What are the basic steps a Javascript engine performs?

A

Reads/Parses JS
Converts/compiles the script to machine language
Machine code runs

17
Q

What cant JavaScript do wrt browser execution?

A
  • Generally access outside of browser and access to user’s private info and data.
  • From a webpage, may not read/write arbitrary files on hard disk.
  • No direct access to OS functions.
  • Tabs/Windows generally don’t know about each other./ This has to do with the same origin policy. there has to be some sort of agreemebt where both pages allow the script to work.
  • CORS - needs explicit permissions.
18
Q

When adding the tag <script>, why do we not use the type and language attirbutes anymore?

A

dont use language because Javascript is the default.
dont use type because its just old and it has a different meaning in modern browsers.

19
Q

What is the history of ECMAScript?

A
  • Netscape needed a script language to make pages more dynamic.
  • They wanted to use Java, but that didnt work so they decided so create something symanitcally similar to Java. Mocha was created.
  • In December 1995, Sun Microsystems and Netscape announced JavaScript in a press release.
  • Microsoft reverse engineered Javascript and called it Jscript because IE came out. 1996.
  • There were now two languages. People realised they needed to standardise this because the internet was growing rapidly.
  • In November 1996, Netscape announced a meeting of the Ecma International standards organization to advance the standardization of JavaScript.
  • ECMA has been a neurtal company to set standards for a while so this is where it started. European Computer Manufacturers Association
  • The ECMAScript specification is a standardized specification of a scripting language developed by Brendan Eich of Netscape; initially named Mocha, then LiveScript, and finally JavaScript.
  • The first edition of ECMA-262 was adopted by the Ecma General Assembly in June 1997. Several editions of the language standard have been published since then.
  • The name “ECMAScript” was a compromise between the organizations involved in standardizing the language, especially Netscape and Microsoft, whose disputes dominated the early standards sessions.
  • Eich commented that “ECMAScript was always an unwanted trade name that sounds like a skin disease.

ECMAScript has been formalized through operational semantics by work at Stanford University and the Department of Computing, Imperial College London for security analysis and standardization.

https://www.youtube.com/watch?v=Sh6lK57Cuk4

20
Q

How does Javascript execution work?

A
  • Code gets parsed by the engine.
  • An Abstract Syntax Tree gets generated.
    • Abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of source code written in a programming language
  • The compiler uses the AST to create machine code.
  • In the compilation process there are optimisations.

Assembly code is like an inbetween step that you can still read but is ready for machine code execution.

21
Q

What is the global object in JS?

A
  1. The global object is an object that always exists in the global scope.
  2. In JavaScript, there’s always a global object defined.
  3. The global object holds variables that should be available everywhere2. That includes JavaScript built-ins, such as Array.
  4. The global scope consists of the properties of the global object, including inherited properties, if any.
  5. In a web browser, when scripts create global variables defined with the var keyword, they’re created as members of the global object
22
Q

What is a javascript event?

A
  • In JavaScript, an event is an action that occurs as a result of user interaction with a web page or application1.
  • JavaScript’s interaction with HTML is handled through events that occur when the user or the browser manipulates a page1.
  • When the page loads, it is called an event1. When the user clicks a button, that click too is an event1. Other examples include events like pressing any key, closing a window, resizing a window, etc.1
23
Q

What is [object window]

A

The window object in JavaScript represents the current web page that is being seen in a browser window. It gives access to the browser’s methods and attributes as the global object in the browser environment1. The window object is available in JavaScript of all browsers2

The window object in JavaScript is a global interface (object type) which is used to control the browser window lifecycle and perform various operations on it1. It represents the browser’s window and all global JavaScript objects, functions, and variables automatically become members of the window object2. Here are some things you can do with it:

Handle right-clicks in the browser — the context menu
The design mode of your document
Copy something to the users clipboard with code
Get the status of the document
Working with the focus / active element
Reading the current styling of an element
Working with the size of the browser & the website displayed3