Js Basics Flashcards

1
Q

Javascript engine

A

Ideally interprets the code
But modern browsers do just in time compilation which leads to better performance

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

String functions

A
charAt()
includes()
startsWith(), endsWith()
toUpperCase()
substr(startIndex, count)
slice(startIndex, endIndex)
string.padStart(5, '-') - makes the length to 5 adding - as prefix
trim(), trimStart(), trimEnd()
split() - converts to array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Map

A
const map = new Map()
map.set(key, value)
map.get(key)
map.size
map.delete(key)
map.clear()

map.keys()
map.values()
map.entries()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Set

A
const set = new Set()
set.add(value)
set.has(value)
set.size
set.delete(value)

set.entries()
set.values()
set.keys()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

error handling

A
throw  'my error'
throw new Error('my error')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

console

A

console.error
console.debug
console.table
console.count(‘key’), console.countReset
console.time(), console.timeLog(), console.timeEnd

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

strict mode

A

‘use strict’;
can be used at function level also

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

dom content loaded

A

window.addEventListener('DOMContentLoaded', ()=>{})

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

difference between var, let

A

var is hoisted and scoped to function
let is hoisted but in dead temporal zone so not accessible also it is block scoped

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

Array

A
const arr = new Arr(10).fill(0) - fill the array of size 10 with 0s
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Data types in js

A
  • Seven primitive data types:
    • numberfor numbers of any kind: integer or floating-point, integers are limited by±(2531).
    • bigintfor integer numbers of arbitrary length.
    • stringfor strings. A string may have zero or more characters, there’s no separate single-character type.
    • booleanfortrue/false.
    • nullfor unknown values – a standalone type that has a single valuenull.
    • undefinedfor unassigned values – a standalone type that has a single valueundefined.
    • symbolfor unique identifiers.
  • And one non-primitive data type:
    • objectfor more complex data structures.

Thetypeofoperator allows us to see which type is stored in a variable.

  • Usually used astypeof x, buttypeof(x)is also possible.
  • Returns a string with the name of the type, like"string".
  • Fornullreturns"object"– this is an error in the language, it’s not actually an object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Type conversions

A

The three most widely used type conversions are to string, to number, and to boolean.

String Conversion– Occurs when we output something. Can be performed withString(value). The conversion to string is usually obvious for primitive values.

Numeric Conversion– Occurs in math operations. Can be performed withNumber(value).

The conversion follows the rules:

Boolean Conversion– Occurs in logical operations. Can be performed withBoolean(value).

Follows the rules:

| Value | Becomes… |
| — | — |
| 0,null,undefined,NaN,”” | false |
| any other value | true |

Value | Becomes… |
| — | — |
| undefined | NaN |
| null | 0 |
| true/false | 1 / 0 |
| string | The string is read “as is”, whitespaces (includes spaces, tabs\t, newlines\netc.) from both sides are ignored. An empty string becomes0. An error givesNaN. |

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

Nullish coalescing operator ‘??’

A

The result of a ?? b is:

if a is defined, then a,
if a isn’t defined, then b.
In other words, ?? returns the first argument if it’s not null/undefined. Otherwise, the second one.

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

Strings

A

Strings are immutable
index of
lastIndexOf
Includes()
Startswith
Endswith
Slice(start, end)
LocaleCompare for comparison
Trim - trims from beginning and end

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

Weakmap and weakset

A

Similar to map and set but keys need to be object and allows for garbage collection.

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

XMLHttpRequest

A
const req = new XMLHttpRequest();

req.addEventListener('load', function (){this.responseText})
req.addEventListener('error', function (){this.responseText})

req.open('GET', 'url');
req.send();
17
Q

fetch

A
const headers = new Headers();
headers.append('Content-Type', 'application/json')
const result = await fetch(url, {
method:'POST',
body: JSON.stringify(bodyObject),
headers
})
new FormData(form)
const abortController = new AbortController()
abortController.abort();
fetch(url, {signal: abortController.signal})
18
Q

Web workers

A

A browser API for running scripts in a separate thread from the main execution thread.

A worker object is created with the Worker(filePath) constructor function. The argument to this function is a path to another JavaScript file that will run in a separate thread.

Workers can send messages back and forth with the main thread via the postMessage(message) method and the onmessage event. For example:

// main JavaScript file
const worker = new Worker('worker.js');
worker.postMessage('hello');
worker.addEventListener('message', (event) => {
  console.log(event.data); // 'world'
});
// worker.js
postMessage('world');
addEventListener('message', (event) => {
  console.log(event.data); // 'hello'
});

In general, most workers are dedicated workers, meaning they can only communicate with the script that created them. However, a SharedWorker can also be created to share a worker with multiple tabs or iframes. That said, SharedWorkers still do not have widespread support across browsers.

19
Q

Custom event

A
// Create the custom event
const myEvent = new CustomEvent('myCustomEvent', {
  detail: { 
    message: 'Hello, world!' 
  }
});

// Add an event listener for the custom event
document.addEventListener('myCustomEvent', function(event) {
  console.log('Custom event triggered:', event.detail.message);
});

// Dispatch the custom event
document.dispatchEvent(myEvent);