TYPES, VALUES, and VARIABLES Flashcards
1) What type of programming language is javaScript?:-
javascript is a high-level, dynamic, interpreted programming language that is well-suited for object-oriented and functional programming styles.
-> javascript variables are untyped.
2) How do you run a js file with node?:-
$ node filename.js (in terminal)
3) What is a programming language’s(e.g javaScript) lexical structure?:-
it is the set of elemenary rules that specify how you write programs in that language. (aka) the lowest-level of synax of a language.
4) What are the types of comments in js:-
single-line(//) multi-line(/**/) (Comments may not be nested)
5) There are three exceptions to the general rule that javaScript interprets line breaks as semicolons when it cannot parse the second line as a continuation of the statement in the first line?:-
-> The first exception invloves the return, throw, yeild, break and continue statement.(These statements often stand alone, but sometime are followed by an identifier or expression.)
-> The second exception involves the ++ and −− operators.
-> The third excep‐ tion involves functions defined using concise “arrow” syntax: the => arrow itself must appear on the same line as the parameter list.
6) JavaScript type are divided into two categories: namely?:-
primitive types and objects types.
* Primitives are immutable: there is no way to change (or “mutate”) a primitive value.
* Primitives are also compared by value: two values are the same only if they have the same value.
7) JavaScript primitive types include (name 3)?:-
numbers, strings(Strings of text) and boolean truth values
* Primitives are immutable: there is no way to change (or “mutate”) a primitive value.
* Primitives are also compared by value: two values are the same only if they have the same value.
8) JavaScript has two additional primitive types that are not numbers, strings or booleans. Name them?:-
the special values null and undefined. (each value is considered to be the sole member of its own special type)
* Primitives are immutable: there is no way to change (or “mutate”) a primitive value.
* Primitives are also compared by value: two values are the same only if they have the same value.
9) In ES6 Symbol was added as a?:-
new special-purpose type, that enables the definition of language extensions
without harming backword compatibility.
* Symbols were introduce to serve as no-string property names.
* To understand Symbols, you need to know that JavaScript’s fundamental Object type is an unodered collection of
properties, where each property has a name and a value. Property names are typically
(and until ES6, were exclusively) strings. But in ES6 and later, Symbols can also serve this purpose:
let strname = “string name”; // A string to use as a property name
let symname = Symbol(“propname”); // A Symbol to use as a property name
typeof strname // => “string”: strname is a string
typeof symname // => “symbol”: symname is a symbol
let o = {}; // Create a new object
o[strname] = 1; // Define a property with a string name
o[symname] = 2; // Define a property with a Symbol name
o[strname] // => 1: access the string-named property
o[symname] // => 2: access the symbol-named property
- The Symbol type does not have a literal syntax. To obtain a Symbol value, you call the Symbol() function.
- This function never returns the same value twice, even when called with the same argument.
This means that if you call Symbol() to obtain a Symbol value, you can safely use that value as a property
name to add a new property to an object and do not need to worry that you might be overwriting an
existing prop‐erty with the same name. Similarly, if you use symbolic property names and
do not share those symbols, you can be confident that other modules of code in your program will
not accidentally overwrite your properties. - The Symbol() function takes an optional string argument and returns a unique Sym‐ bol value.
If you supply a string argument, that string will be included in the output of the Symbol’s toString() method.
Note, however, that calling Symbol() twice with the same string produces two completely different Symbol values.
let s = Symbol(“sym_x”);
s.toString() // => “Symbol(sym_x)”
10) What is an object?:-
any javaScript value that is not a number, string, boolean, null, undefined
or Symbol (that is a member of the type object).
11) What is an ordinary javaScript objetc?:-
is an unordered collection of named values.
12) JavaScript defines a special kind of object called?:-
an array (an ordered collection of numbered values)
13) In addition to basic/ordinary objects and arrays javaScript defines other useful types of object. Name
those 5 objects?:-
-> A Set object respesents a set of values.
-> A Map object represents a mapping from values to keys.
-> Various “typed array” types facilitate operations on arrays of bytes and other binary data.
-> The RegExp type represents textual patterns and enables sophisticated matching, searching, and replacing
operations on strings.
-> The Date type represents dates and times and supports rudimentary date arthimetics.
-> Error and its subtypes represents errors that can arise will running javaScript code.
14) In javaScript which two values can method not be invoked on?:-
null and undefined.
15) JavaScript objects are mutable and its primitive types are immutable. what does that mean?:-
-> Mutable types can change.
-> Immutable types cannot change. (Strings can be thought of as arrays of characters but they are immuntable)
16) JavaScript constants and variables are untyped. What does that mean?:-
declaractions do not specify what kind of values will be assigned.
17) The javaScript number format allows you to exactly represent all integers btw?:-
−9,007,199,254,740,992 (−25^3) and 9,007,199,254,740,992 (25^3), inclusive.
18) What is a numeric literal in js?:-
numbers that appear directly in the js code.
19) What is the structure of a base 16 literal?:-
hexdecimal values start with a 0x or 0X, followed by a string of hexdecimal digints. hexdecimal values go from 0 - 9 and a/A - f/F (10 - 15)
examples
0xff // => 255: (15*16 + 15 * 16)
0xBADCAFE // => 195939070
20) ES6 introduces binary and octal interger representations as:-
-> base 2 (0b or 0B) 0b10101 // => 21: (116 + 08 + 14 + 02 + 11)
-> base 8 (0o or 0O) 0o377 // => 255: (364 + 78 + 71)
21) Arthimetics in javaScript does not raise errors in case of overflow, underflow and division by zero.
What definitions of js hand this three cases?:-
-> When the result of a numeric operation is larger than the largest repre‐ sentable number (overflow),
the result is a special infinity value, Infinity. Similarly, when the absolute value of a negative value becomes
larger than the absolute value of the largest representable negative number, the result is negative infinity, -Infinity.
* The infinite values behave as you would expect: adding, subtracting, multiplying,
or dividing them by anything results in an infinite value (possibly with the sign reversed).
-> Underflow occurs when the result of a numeric operation is closer to zero than the smallest representable number.
In this case, JavaScript returns 0. If underflow occurs from a negative number, JavaScript returns a special value known as “negative zero.”
-> Division by zero is not an error in JavaScript: it simply returns infinity or negative infinity. There is one exception,
however: zero divided by zero does not have a well- defined value, and the result of this operation is the special not-a-number value, NaN.
* JavaScript predefines global constants Infinity and NaN to hold the positive infinity and not-a-number value,
and these values are also available as properties of the Number object:
Infinity // A positive number too big to represent
Number.POSITIVE_INFINITY // Same value
1/0 // => Infinity or NaN
Number.MAX_VALUE * 2 // => Infinity; overflow
NOTE: adding a negtive sign in the example above gives negative infinity.
Number.MIN_VALUE/2 // => 0: underflow
-Number.MIN_VALUE/2 // => -0: negative zero
-1/Infinity // -> -0: also negative 0
-0
22) What does NaN stand for?:-
not-a-number
23) When does NaN(not-a-number) arise?:-
-> division by zero(can also return -Infinity(negative infinity)).
-> dividing Infinity by Infinity.
-> taking the square root of a negative number.
-> using arthimetic operations on non-numeric operands that cannot be coverted to numbers.
NaN // The not-a-number value
Number.NaN // The same value, written another way
0/0 // => NaN
Infinity/Infinity // => NaN
24) What is the unsual feature of the NaN value?:-
It does not compare equal to any other value, including itself.
25) You can write x === NaN to determine if the value of x is NaN. Instead what are the two options for checking if x is equal to NaN?:-
x != NaN or Number.isNaN (These expressions wil be true if and only if, x has the
same value as the global constant NaN)
26) The global function isNaN() is similar to Number.isNaN(). What does it return?:-
it returns true if its argument is NaN, or it that argument is non-numeric value that cannot be converted to
a number.
27) What does Number.isFinite() return?:-
it return true if its argument is a number other than NaN, Infinity,
or -Infinity.
28) The global isFinite() function returns?:-
true if its argument is a finite number or can be converted to a finite number.
29) What is the unusual behavior of negative zero in relation to positive zero?:-
it compares equal
(even when using strick equal test “===”) to postive zero, The two values are almost indistinguishable,
except when used as a divisor:
let zero = 0; // Regular zero
let negz = -0; // Negative zero
zero === negz // => true: zero and negative zero are equal
1/zero === 1/negz // => false: Infinity and -Infinity are not equal
30) What is the explation fo
let x = .3 - .2; // thirty cents minus 20 cents
let y = .2 - .1; // twenty cents minus 10 cents
x === y // => false: the two values are not the same!
x === .1 // => false: .3-.2 is not equal to .1
y === .1 // => true: .2-.1 is equal to .1
?:-
Binary Floating-Point and Rounding Errors
- Binary floating-point representations cannot exactly represent numbers as simple as 0.1.
- If these floating-point approximations are problematic for your programs,
consider using scaled integers. For example, you might manipulate monetary values as integer
cents rather than fractional dollars.
31) As of ES2020 BigInt(Numeric type whose values are integers) was introduce for what purpose?:-
was add to js maily to allow the representation of 64-bit intergers, which are required by many other
programming languages and APIs.
32) Why are BigInt not good for cryptography?:-
because they do not attempt to prevent timing attacks.
33) What is the synax of a BigInt literal?:-
BigInt literals are written as strings of digits followed by
a lowers case ‘n’. By default they are in base 10, but you can use 0X, 0O, or 0B prefixes.
1234n
0b111111n
0o7777n
0x8000000000000000n // => 2n**63n: A 64-bit integer
34) What is BigInt() function used for?:-
for converting regular javaScript nnumbers and strings to
BigInt values.
BigInt(Number.MAX_SAFE_INTEGER) // => 9007199254740991n
let string = “1” + “0”.repeat(100); // 1 followed by 100 zeros.
BigInt(string) // => 10n**100n: one googol
- Arithmetic with BigInt values works like arithmetic with regular JavaScript numbers,
except that division drops any remainder and rounds down (toward zero)
1000n + 2000n // => 3000n
3000n - 2000n // => 1000n
2000n * 3000n // => 6000000n
3000n / 997n // => 3n: the quotient is 3
3000n % 997n // => 9n: and the remainder is 9
(2n ** 131071n) - 1n // A Mersenne prime with 39457 decimal digits
- Although the standard +, -, *, /, %, and ** operators work with BigInt,
it is important to understand that you may not mix operands of type BigInt with regular number operands.
35) What does the Date class do?:-
it represents and manipulates the numbers that represent date and time.
Date are objects but also have a numerical representation as timestamps, that represent the number of millisseconds
that have elapsed since January 1, 1970.
let timestamp = Date.now(); // The current time as a timestamp (a number).
let now = new Date(); // The current time as a Date object.
let ms = now.getTime(); // Convert to a millisecond timestamp.
let iso = now.toISOString(); // Convert to a string in standard format.
36) What is a string?:-
its the javaScript type for representing text.
* Strings are immuntable ordered sequence of 16-bit values(each character is 16-bit -check if correct-).
* The length of a string isthe number of 16-bit values it contains
* JavaScript does not have a special type for repsenting a single element of a string (character).
To represent a single 16-bit value, simple use a string of length 1.
* As of ES6 strings are iterable(iteration happens on the actual characters of the string, not the 16-bit values.)