Basic JS Flashcards
What is the difference between
Loose equality (==and !=)
Strict equality (=== and !==)
Give examples of each.
Both are used for comparison between two variables:
Loose equality: == compares whilst ignoring the Type of variable (checks for value) - AVOID - produces unexpected results. e.g. 5=="5" (true) "" == "0" (false) 0 == "" (true)
Strict equality:
=== compares whilse checking the datatype AND comparing the two values.
(checks for value + type)
e.g. 5===”5” (false)
What is the “Unary +” Operator? e.g. +hello
It attempts to convert to a number. +v is the same as Number(v)
”$” + 4 + 5 =
“$45”
” \t \n” - 2 =
-2
Whats the difference between NULL and Undefined?
When are they used?
What is the typeOf null and typeOf undefined
NULL = Non existing | Null pointe Null us a separate type of it's own that contains only the null value. Javascript never sets a value to null by default. typeof === Object (this is a very old JS bug that was decided to be left in) let myVariable = null;
Undefined = Value declared but not assigned a value
typeof === undefined
let myVariable;
Usually use null to assign empty or unknown values and undefined for checks to see whether a variable has been assigned.
What are the eight data types of javascript?
- number -for numbers of any kind: integer or floating-point, integers are limited by
±(2531)
. - bigint -is for integer numbers of arbitrary length.
- string - for strings. A string may have zero or more characters, there’s no separate single-character type.
- boolean -for
true
/false
. - nullfor unknown values – a standalone type that has a single value
null
. - undefinedfor unassigned values – a standalone type that has a single value
undefined
. - objectfor more complex data structures.
- symbolfor unique identifiers.
What is a primitive data type, which data type is NOT primitive?
“primitive” values can contain only a single thing (be it a string or a number or whatever)
Objects - are NOT primitive they are used to store collections of data and more complex entities.
What is the difference between substring/substr/slice. Which one is considered a “legacy function”?
All of them returns the portion of the string that starts at indexStart and ends the character immediately preceding indexEnd (DOES NOT RETURN THE LAST ONE, RETURNS THE ONE BEFORE IT)
str.substring(indexStart, indexEnd);
If indexStart or indexEnd is less than 0, it is treated as 0.
If indexEnd < indexStart, the two are swapped.
substr () - legacy, don't use in new code. substr is considered a "legacy function" in Mozilla's docs. You shouldn't use it when writing new code. Unlike substring(), you can call substr() with a negative start
str.slice(beginIndex, endIndex)
Best of both worlds between slice and substring
-Supports negative indices
What are the three logical operators and what do they stand for?
|| && !
What are truthy and falsy values? and What are the 6 falsy values in JavaScript?
truthy || falsy values are values that is considered true or false when encountered in a Boolean context.
6 falsy values = ‘Empty Quotes’ 0 NaN undefined null false
What are the two ways to convert a value to a boolean?
!! and Boolean()
Is Javascript Statically Typed, or Dynamically typed? What do they mean?
JS is Dynamically typed: a variable can change from one type to another. Not statically typed like “int number = 5”
What are the types of Exports? How are they exported and imported?
Named Exports (0 or more exports per module) Named exports are exported as an object, so they are DESTRUCTURED ON IMPORT. export const arrayOne = [1,2,3] export const arrayTwo = [4,5,6] import {arrayOne, arrayTwo} from "./locationWherever"
Default Exports (1 export per module)
const arrayOne = [1,2,3] export default arrayOne; import arrayOne from "./locationWherever"