Introduction Flashcards

1
Q

Programs in JS are called what ?

A

Scripts

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

Can we write JS scripts directly into the html ?

A

They can be written right in a web page’s HTML and run automatically as the page loads.

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

Do scripts need compilation before running ?

A

Scripts are provided and executed as plain text. They don’t need special preparation or compilation to run.

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

What we need to run a JS program ?

A

Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.

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

The browser has an embedded engine sometimes called ?

A

“JavaScript virtual machine”.

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

Examples of some JS engines in browsers today ?

A

V8 – in Chrome, Opera and Edge.
SpiderMonkey – in Firefox.
…There are other codenames like “Chakra” for IE, “JavaScriptCore”, “Nitro” and “SquirrelFish” for Safari, etc.

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

Briefly explain how the JS engine works ?

A

Engines are complicated. But the basics are easy.

The engine (embedded if it’s a browser) reads (“parses”) the script.
Then it converts (“compiles”) the script to the machine language.
And then the machine code runs, pretty fast.
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.

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

Do JS provides low-level access to memory to CPU ?

A

Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.

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

can we do webpage manipulation, interaction with the user and webserver with in-browser javascript ?

A

Yes

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

How to add new HTML to the page, change the existing content, modify styles.

A

we can do it with in browser js

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

Which type of js can React to user actions, run on mouse clicks, pointer movements, key presses.

A

In-browser JS

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

Which JS can Send requests over the network to remote servers, download and upload files (so-called AJAX and COMET technologies).

A

IN browser JS

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

Which JS can Get and set cookies, ask questions to the visitor, show messages.

A

In browser JS

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

Which JS can Remember the data on the client-side (“local storage”).

A

In browser JS

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

can In browser JavaScript on a webpage read/write arbitrary files on the hard disk

A

JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS functions.

Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like “dropping” a file into a browser window or selecting it via an tag.

There are ways to interact with camera/microphone and other devices, but they require a user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the NSA.

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

What is same origin policy ?

A

Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).

This is called the “Same Origin Policy”. To work around that, both pages must agree for data exchange and contain a special JavaScript code that handles it. We’ll cover that in the tutorial.

This limitation is, again, for the user’s safety. A page from http://anysite.com which a user has opened must not be able to access another browser tab with the URL http://gmail.com and steal information from there.

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

can two different sites(having different servers) communicate with each other using JS

A

JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that’s a safety limitation.

Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.

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

How languages over JS like typescript or coffeeScript works ?

A

The syntax of JavaScript does not suit everyone’s needs. Different people want different features.

That’s to be expected, because projects and requirements are different for everyone.

So recently a plethora of new languages appeared, which are transpiled (converted) to JavaScript before they run in the browser.

Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it “under the hood”.

19
Q

What is the significance of script tag ?

A

The tag contains JavaScript code which is automatically executed when the browser processes the tag.

20
Q

Where is the file located ?

src=”picture.jpg”

A

The “picture.jpg” file is located in the same folder as the current page

21
Q

Location of the file ?

src=”images/picture.jpg”

A

The “picture.jpg” file is located in the images folder in the current folder

22
Q

Location of the file ?

src=”/images/picture.jpg”

A

The “picture.jpg” file is located in the images folder at the root of the current web

23
Q

Location of the file ?

src=”../picture.jpg”

A

The “picture.jpg” file is located in the folder one level up from the current folder

24
Q

What is absolute file paths

A

An absolute file path is the full URL to a file:

25
Q

What is relative file paths ?

A

A relative file path points to a file relative to the current page.
eg- <img></img><img></img><img></img>

26
Q

What is the best practice while making file paths ?

A

It is best practice to use relative file paths (if possible).

When using relative file paths, your web pages will not be bound to your current base URL. All links will work on your own computer (localhost) as well as on your current public domain and your future public domains.

27
Q

Why external script files are used in large projects ?

A

As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.

The benefit of a separate file is that the browser will download it and store it in its cache.

Other pages that reference the same script will take it from the cache instead of downloading it, so the file is actually downloaded only once.

That reduces traffic and makes pages faster.

28
Q

What if we have both code inside the script tag and also the src attribute pointing to an external js file ?

A

A single tag can’t have both the src attribute and code inside.

This won’t work:

alert(1); // the content is ignored, because src is set

We must choose either an external or a regular

 with code.

The example above can be split into two scripts to work:

<script>
alert(1);
</script>
29
Q

Are semicolons necessary in JS after each statement ?

A

Line breaks with semicolons or without it, both works

JavaScript interprets the line break as an “implicit” semicolon. This is called an automatic semicolon insertion.

30
Q

Why we add ‘use strict’ at the top of script files ?

A

For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn’t change.

That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript’s creators got stuck in the language forever.

This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: “use strict”.

31
Q

How to cancel use strict on script files ?

A

There is no directive like “no use strict” that reverts the engine to old behavior.

Once we enter strict mode, there’s no going back.

32
Q

Should we use “use strict”?

A

The question may sound obvious, but it’s not so.

One could recommend to start scripts with “use strict”… But you know what’s cool?

Modern JavaScript supports “classes” and “modules” – advanced language structures (we’ll surely get to them), that enable use strict automatically. So we don’t need to add the “use strict” directive, if we use them.

So, for now “use strict”; is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.

33
Q

What is dynamically typed ?

A

Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them.

Number
let n = 123;
n = 12.345;
The number type represents both integer and floating point numbers.
34
Q

What is NaN ?

A

NaN represents a computational error. It is a result of an incorrect or an undefined mathematical operation, for instance:

alert( “not a number” / 2 ); // NaN, such division is erroneous

35
Q

Can we do mathematical operations with NaN

A

NaN is sticky. Any further mathematical operation on NaN returns NaN:

alert( NaN + 1 ); // NaN
alert( 3 * NaN ); // NaN
alert( “not a number” / 2 - 1 ); // NaN
So, if there’s a NaN somewhere in a mathematical expression, it propagates to the whole result

36
Q

Value of this NaN ** 0

A

1

37
Q

Do divide by zero, treat non-numeric strings as numbers, etc. throws an exception in JS

A

Mathematical operations are safe
Doing maths is “safe” in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.

The script will never stop with a fatal error (“die”). At worst, we’ll get NaN as the result.

38
Q

What is bigInt ?

A

In JavaScript, the “number” type cannot represent integer values larger than (2^53-1) (that’s 9007199254740991), or less than -(2^53-1) for negatives. It’s a technical limitation caused by their internal representation.

39
Q

Is there character data type in JS ?

A

There is no character type.
In some languages, there is a special “character” type for a single character. For example, in the C language and in Java it is called “char”.

In JavaScript, there is no such type. There’s only one type: string. A string may consist of zero characters (be empty), one character or many of them.

40
Q

What is null in JS

A

The special null value does not belong to any of the types described above.

It forms a separate type of its own which contains only the null value:

let age = null;
In JavaScript, null is not a “reference to a non-existing object” or a “null pointer” like in some other languages.

It’s just a special value which represents “nothing”, “empty” or “value unknown”.

The code above states that age is unknown.

41
Q

What is undefined in JS ?

A

The special value undefined also stands apart. It makes a type of its own, just like null.

The meaning of undefined is “value is not assigned”.

If a variable is declared, but not assigned, then its value is undefined:

let age;

alert(age); // shows “undefined”

42
Q

Basic definition of object in JS

A

All other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities.

43
Q

Output of typeof null ?

A

The result of typeof null is “object”. That’s an officially recognized error in typeof, coming from very early days of JavaScript and kept for compatibility. Definitely, null is not an object. It is a special value with a separate type of its own. The behavior of typeof is wrong here.