JavaScript (Duckett) chap 2 Flashcards
What can be included in statements?
Lol anything and everything:
Data (they definetely need these foundations)
Array (helps organize data numerically)
Variables (help store data and makes use easy)
Code block (contain variables & expressions)
Expressions (way of making var work together)
Methods (var & methods used to accom. tasks)
What is a codeblock
A codeblock is represented by parentheses { }.
These hold statements, usually for methods (like the if method). When you think code block just thing a series of statements that work best inside a method, so a method can be called and the code block can be easily utilized.
How would you create a variable?
How to put values in it?
How to use it?
You declare a variable into being when you create it so in Js you write: var A;
Kinda like screaming VARIABLE A! But instead of the exclamation you have a semicolon because that’s how you end a statement.
To put a value inside a variable you assign a value to the variable by using the assignment operator (=). Very very simple: A = 2;
To use the variable you need to understand scope but mainly simply include the var. name.
Variables are ways of storing data, what are the data types available to store in variables?
6 data types:
Booleans (true, false)
strings (text in quotes)
numbers (integers, real num, floating ., sci not)
null ( zero value where they used to be or not)
undefined data (never assigned a value)
Object (complex data like arrays & methods)
What is an assignment operator
He equals sign (=). You’ll see the assignment operator whenever you want to assign a value to a variable of some kind. You’ll use it a lot. Operators are things that help programmers write mathematical expressions easily and quickly.
An operator is used to help programmers write expressions quickly and easily. What are examples of operators?
+ - x / = are all mathematical operators used with data, the last one being used to assign values. The plus used to con😺enate or join two data types together.
Js code specific operators are:
– Decrement
++ Increment
% Modulus (divides 2 values, retrns remainder)
+= , -=, *=, /=, %= (quick ways to write exprss)
E.g., x += y is same as saying x=x+y
E.g., ‘leave’ += ‘now.’ is ‘Leave now.’
E.g., t1= ‘stay’, txt1 += ‘now’; t1= ‘stay now’
These are assignment operators (like = )
String operators are + (and only +).
Comparison & logical operators == (equal to) === (equal value & equal type) != (not equal) !== (not equal value or equal type) >, = ,
what is scope in relation to variables
functions in programming are a world unto their own, they exist in their own sphere. Whereas everything outside a function is considered global. So if you declare something in a function it is a function-level variable which is also called a local variable. Always declare your variable in your function with the var keyword. If it is not DECLARED it is in the global scope.
If a variable is declared outside the local-level (lol) then it’s a global variable in the global scope. Global variables can be used inside functions, and can even have the same name as a local variable but in this case they should not be used inside a global scope.
Talking about strings how would you escape a quote in a string data type in javascript?
You’d escape it using forward slash just before the quotecyiu want to escape.
When naming your variables what’s the general rule to use.
Use camelCase, which means to use lower case letters only except for when starting a new word then you use a upper case letter as the next words first letter.
What is a shorthand way to quickly declare a bunch of variables?
Ways to declare and assign values to variables?
To quickly declare variables you could declare a bunch of variables at once: var a,b,c;
To declare a variable & assign a value to it: var a = 5;
This is better than declaring a variable in one line, then assigning a value to it in another line.
An array is a way to store data in your variable (arrays are objects and are also a data type, as are objects). What characters are used to type in an array into your text file.
Arrays use square brackets [ ]. In Js you’ll use the brackets when you create, access or use arrays. Very easy.
So you’s say var a = [‘one’,’two’,3];
Arrays are great because they bring a numeric order to your data items. Arrays call their items indexes, starting with index item one. To access or use an array you’d enter a[0]
What are expressions?
Expressions evaluate into a value. Math can’t be done without expressions, coding can’t be done easily without expressions. They simply utilize operators to give a new value. An example of a popular mathematical expression is 1 + 2. The value of this expression being its solution.
A = 1+2, the A is a variable (not the expression)
5 kinds of operators are
Arithmetic operators String operators Assignment operators Comparison operators Logical operators
A script of text is
A set of instructions for the browser to follow. Typically line by line and each line is a statement, like a sentence, every statement ends with a semicolon (;).