JavaScript Bits and Values

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What are the Undefined Values

null and undefined, used to denote the absence of a meaningful value. They are themselves values, but they carry no information. Note that NaN is not a value. They are almost interchangeable.

Name the basic values of JS

numbers 123, "strings", booleans (true, false), and undefined values (null, undefined)

Types of \ escapes for strings

\t within quotes means a tab \n within quotes means a newline (i.e., indent paragraph) \"" within quotes makes quotes within quotes

What is a bit

a bit is any two-valued thing (1, 0) (shiny, dull), (strong, weak) -- EVERY kind of discrete info can be reduced to a 1, 0 sequence, i.e., represented by bits.

What is the conditional operator (or the only ternary operator in JS)

a three part operator which looks like: console.log(true ? 1 : 2) // → 1 console.log(false ? 1 : 2) // → 2 The value on the left of the question mark "picks" which of the other two values will come out. When it is true, the middle (or left of :, right of ?) value is chosen, and when it is false, the value on the right comes out.

How does binary (machine code) factor x2

from right to left

What are operators? Note: different than values when boolean.

+, -, *, / , % are all operators in JS, part of its arithmetic. Putting an operator between two values will apply it to those values. Boolean operators are ==, ===, =!, ==!, >, >=, <, <= Logical operators are ||, !, and &&

Describe boolean values.

Boolean type operators produce either a yes, or a no, an A-OK, or no-jose! ... or, rather, just a true and false. This is important to understand: the "operator" > in example 1 below gives the boolean "value" true. The second "operator" > gives the boolean "value" false. 1. console.log (3 > 2) // -> true 2. console.log (2 > 3) //-> false

How can: "A newline character is written like "\n"." be expressed through console.log?

console.log("A newline character is written like \"\\n\".")

What is type coercion?

When an operator is applied to the "wrong" type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren't what you want or expect. This is called type coercion. console.log(8 * null) // → 0 (makes null = 0) console.log("5" - 1) // → 4 (makes string a number: 5 - 4) console.log("5" + 1) // → 51 (makes it concatenated "5" + "1") console.log("five" * 2) // → NaN (can't change string to anything) console.log(false == 0) // → true

binary search

Works better than linear search at finding specific information on a list, such as finding a name, or a number. It works by repeatedly dividing in half the portion of the list that could contain the item.

How do booleans work on strings?

console.log("Aardvark" < "Zoroaster") // → true (the boolean value) The way strings are ordered is more or less alphabetic: uppercase letters are always "less" than lowercase ones, so "Z" < "a" is true, and non-alphabetic characters (!, -, and so on) are also included in the ordering. The actual comparison is based on the Unicode standard. This standard assigns a number to virtually every character you would ever need, including characters from Greek, Arabic, Japanese, Tamil, and so on.

How are fractional numbers written in JS?

with a dot (.) (2.12)

How are super big or super small numbers written in JS?

with exponent notations 2.998e8 That is 2.998 × 108 = 299,800,000.

What is the logical operator && and what does it do?

The && operator represents logical and. It is a binary operator, and its result is true only if both the values given to it are true. console.log(true && false) // → false (a boolean value) console.log(true && true) // → true (boolean) Truth-Table: T T - T T F - F F T - F F F - F

The short-circuit for the logical operator &&

The && operator works similarly to ||, but the other way around. When the value to its left is something that converts to false, it returns that value, and otherwise it returns the value on its right. Another important property of these two operators, || and && is that the expression to their right is evaluated only when necessary. In the case of true || X, no matter what X is—even if it's an expression that does something terrible—the result will be true, and X is never evaluated. The same goes for false && X, which is false and will ignore X. This is called short-circuit evaluation.

bit pattern

when calling for 13 in JS, the computer internally will know only the bit pattern for 13, 00001101, as that is the translation. (if that is how it reads JS bits. I don't know enough about machine code, or even if a 64 vs 32 vs 8 bit has anything to do with this. ^ That 00001101 could be for 8 bits)

00001101 is 13 because?

{ 0 -- 128 0 -- 64 0 -- 32 0 -- 16 1 -- 8 = 8 + 1 -- 4 = 4 + 0 -- 2 1 -- 1 = 1 } = 13

Strings?

"Strings are a basic data type used to show text, when written between quotation marks"

What is the logical operator ! and what does it do?

! is known as not. makes whatever value the opposite of that value, so !true is false, and !false is true. So, it's a unary operator that flips whatever value is given to it.

What is "escaping a character" in a string and how do you do it?

"using \ (backslash) within quotes" to indicate that the character after it has a special meaning. Like: "this is the first line\n and this is the second" Becomes: this is the first line and this is the second so, the character is escaping the normal read of the language

When operators of the same precedence occur next to each other, how does the language determine which goes first?

1 + 2 - 1 = ? from left to right (1 +2 ) - 1 = 2

linear search

A search that starts at one point, on moves through each piece of information to find an item. (Like moving through every name, beginning with the first). Not as efficient in this way as binary search.

how many bits does JS use for a single number?

Exactly and only 64 bits per single number value. Negative numbers are recognized, so 1 bit goes towards the sign of the number (+, -), and 1 bit to fractionals (.).

JS is limited in the amount of numbers that can be represented. How many?

For N decimal digits, the amount of numbers that can be represented is 10N. Similarly, given 64 binary digits, you can represent 264 different numbers, which is about 18 quintillion (an 18 with 18 zeros after it). with fractionals, and negatives, this is ~9 quadrillion (15 zeros).

prompt()

Gives the user a prompt that they can answer

if ... { } else ... { }

If then statement for determining what is going to be the outcome. When used with a logical statement like 3+3 = 3, it's only ever going to come out with one answer; however, with a prompt or variably changing answer, it can give different outputs, per input.

When facing an issue with ==, such as: console.log(false == 0) What does JavaScript do?

In most cases, it just tries to convert one of the values to the other value's type. However, when null or undefined occurs on either side of the operator, it produces true only if both sides are one of null or undefined. console.log(null == undefined); // → true console.log(null == 0); // → false That last piece of behavior is often useful. When you want to test whether a value has a real value instead of null or undefined, you can simply compare it to null with the == (or !=) operator.

It's hard to know where parentheses should go sometimes. Out of all operators, what's the order from weakest to strongest?

In practice, you can usually get by with knowing that of the operators we have seen so far, || has the lowest precedence, then comes &&, then the comparison operators (>, ==, and so on), and then the rest. This order has been chosen such that, in typical expressions like the following one, as few parentheses as possible are necessary: 1 + 1 == 2 && 10 * 10 > 50

Special Numbers?

Infinity, -infinity, and NaN (Not a Number). NaN comes from (0 / 0) or (Infinity - Infinity).

What is %?

Modulo (though remainder is more accurate). It gives the remainder of the operation. So X % Y is the remainder of dividing X by Y. For example, 314 % 100 produces 14, and 144 % 12 gives 0

Can strings be multiplied, divided, added, or subtracted? Can they be concatenated?

No. However, they can be concatenated using the + operator! console.log("con" + "cat" + "e" + "nate") concatenate

Unary operators?

Not all operators are symbols (+, /). typeof is an operator that produces a string value of the type of operator! console.log(typeof 4.5) // → number console.log(typeof "x") // → string console.log(typeof(null)) // → object console.log(typeof(true)) // → boolean console.log(typeof("ken")) // → string

What are logical operators?

Operations that can be applied to Boolean values themselves. JavaScript supports three logical operators: and, or, and not. These can be used to "reason" about Booleans. These are: &&, ||, and !

Describe Unary vs Binary operators.

Operators that use two values are called binary operators, while those that take one are called unary operators. The minus operator can be used both as a binary operator and as a unary operator. console.log(- (10 - 2)) // → -8

What are some boolean operators?

Other similar operators are >= (greater than or equal to), <= (less than or equal to), == (equal to), and != (not equal to). console.log("Itchy" != "Scratchy") // → true

Order of operations? or rather, Precedence?

PEMDAS Paraenthases ( ) Exponents ^N Multiply or Divide (from left to right in problem) Add or Subtract (from left to right in the problem)

console.log ()

Prints whatever is entered in the (), including strings of text "", numbers 3, 4 , 5, variables, etc.

What is the difference between == and === and !== and !== ?

The difference is that === variations show whether something is precisely equal or not. See: But what if you want to test whether something refers to the precise value false? The rules for converting strings and numbers to Boolean values state that 0, NaN, and the empty string ("") count as false, while all the other values count as true. Because of this, expressions like 0 == false and "" == false are also true. For cases like this, where you do not want any automatic type conversions to happen, there are two extra operators: === and !==. The first tests whether a value is precisely equal to the other, and the second tests whether it is not precisely equal. So "" === false is false as expected. console.log("" == false) // -> true console.log("" != false) // -> false console.log("" === false) // -> false console.log("" !== false) // -> true console.log("" !== true) // -> true console.log("" != true) // -> true

What is the logical operator || and what does it do?

The || operator denotes logical or. It produces true if either of the values given to it is true. console.log(false || true) // → true (boolean) console.log(false || false) // → false (boolean) Truth-Table: T T - T T F - T F T - T F F - F

What is NaN equal to in JS?

There is only one value in JavaScript that is not equal to itself, and that is NaN, which stands for "not a number". console.log(NaN == NaN) // → false NaN is supposed to denote the result of a nonsensical computation, and as such, it isn't equal to the result of any other nonsensical computations.

Short-circuiting for the logical operator ||

They will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they return either the original left-hand value or the right-hand value. The || operator, for example, will return the value to its left when that can be converted to true and will return the value on its right otherwise. This conversion works as you'd expect for Boolean values and should do something analogous for values of other types. note: Boolean Value's are -- > true, false console.log(null || "user") // → user console.log(undefined || "user") // → user console.log("Ken" || "user") // kicks out "Ken", because Ken is on the left, //and that's the one || short-circuits to This functionality allows the || operator to be used as a way to fall back on a default value. If you give it an expression that might produce an empty value on the left, the value on the right will be used as a replacement in that case!!

What is short-circuiting values for logical operators && and ||?

Values which aren't "normal" for logical operators (strings, numbers), they will convert the value on their left side to Boolean type in order to decide what to do, but depending on the operator and the result of that conversion, they return either the original left-hand value or the right-hand value.


संबंधित स्टडी सेट्स

Foundations of Gerontology and Theories of Aging, Prep U: Culture, Spirituality, Etc.

View Set

Chapter 6 - Differentiation Strategy and Cost Leadership Strategy

View Set

Combo with "EMT Practice Final Examination" and 21 others Sue R

View Set

Proteins and Enzymes: Adv. Biology

View Set

Third-Party Ownership and Insurable Interest

View Set

Networking Plus Chapter Three, Network Ch.3, Network+ Ch3 quiz, Networking Final Quizlet

View Set