Javascript Basics
260 flashcards · Programming Languages · by Mae000
Java Script file extension?
.js
External Javascript
javascript code written separately in order to be brought into a document <script src="javascript.js"> <script
Function of console.log()
To print something onto the console
Variable Definition?
variables are simply “storage containers” for data in your code.
Ways to create variable in JS?
var let const
Difference between the 3 ways to write a variable
let and const are newer than var and behave differently in some aspects.
Recommended ways to write variables
let and const
How to assign a variable value
Using the using the assignment operator =
What does a string look like?
'Hello!';
Multi-line or single-line variables declarations?
Multi-line cus readability
True or False: You can only change a variable's value 20 times
False. Infinite changes. Old Value is thrown away
A variable has to be unique but can share values
Bla Bla Black Sheep
How many time should a variable be declared
Once or else error
Example of copying data from one variable into the other.
message = hello;
Name the limitations on naming variable
The name must contain only letters, digits, or the symbols $ and _. The first character must not be a digit.
In JS Case Matters
Apple and apple are very different
What names can't be used for variables?
Reserved names
Whats the function of the const?
To create an unchanging value
How to reassign values?
declare without let
Purpose of Upper-cased Constant
Hard coded values like colors
Rules for naming Variables
Simple and Descriptive name Avoid Abbr.
When should const not be upper case
When its not hardcoded, when u desire change, when its value is decided in run time
+
Addition
-
Subtraction
*
Multiplication
**
Exponentiation
/
Division
%
Modulus (Remainder)
++
Increment
--
Decrement
Two numbers can be consider literals or variables or expressions
# a
What are operands?
The numbers (in an arithmetic operation)
The addition operator (+) adds numbers
#
The subtraction operator (-) subtracts numbers.
The multiplication operator (*) multiplies numbers.
The division operator (/) divides numbers.
The modulus operator (%) returns the division remainder.
The increment operator (++) increments numbers.
increases by +1
The decrement operator (--) decrements numbers.
Decreases by -1
The exponentiation operator (**) raises the first operand to the power of the second operand.
Operator precedence describes the order in which operations are performed in an arithmetic expression.
Pemdas
JavaScript has only one type of number. Numbers can be written with or without decimals.
Extra large or extra small numbers can be written with scientific (exponent) notation:
let x = 123e5; // 12300000 let y = 123e-5; // 0.00123
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
Floating point arithmetic is not always 100% accurate:
If you add two numbers, the result will be a number:
-Numbers are added. Strings are concatenated.
If you add a number and a string, the result will be a string concatenation:
JavaScript will try to convert strings to numbers in all numeric operations:
NaN is a JavaScript reserved word indicating that a number is not a legal number.
But numbers can also be defined as objects with the keyword new:
let x = 123; let y = new Number(123); The new keyword complicates the code and slows down execution speed.
Comparing two JavaScript objects always returns false.
Integers are floating-point numbers without a fraction. They can either be positive or negative.
e.g. 10, 400, or -5.
Floating point numbers (floats) have decimal points and decimal places.
for example 12.5, and 56.7786543.
Doubles are a specific type of floating point number that have greater precision than standard floating point numbers (meaning that they are accurate to a greater number of decimal places).
Binary
The lowest level language of computers; 0s and 1s.
Octal
Base 8, uses 0–7 in each column.
Hexadecimal
Base 16, uses 0–9 and then a–f in each column. You may have encountered these numbers before when setting colors in CSS.
How many data types does Java script have for numbers and benefits?
Numbers and it means all data types could be handled the same way
JS second number type
BigInt used for incredibly large Integers
typeof operator
returns the data type
toFixed() method
to round your number to a fixed number of decimal places,
Convert String to Number
Number (String)
Position of the increment value
At front (increment or decrement then returns). At back (need to rerun to give new value)
Assignment operators
operators that assign a value to a variable.
=
it assigns the variable on the left the value stated on the right
+=
Addition assignment: Adds the value on the right to the variable value on the left, then returns the new variable value (x += 4;)
-=
Subtraction assignment: Subtracts the value on the right from the variable value on the left, and returns the new variable value (x -= 3;)
*=
Multiplication assignment: Multiplies the variable value on the left by the value on the right, and returns the new variable value (x *= 3;)
/=
Division assignment: Divides the variable value on the left by the value on the right, and returns the new variable value (x /= 5;)
comparison operators.
run true/false tests, compare values
===
Strict equality: Tests whether the left and right values are identical to one another (5 === 2 + 4)
!==
Strict-non-equality: Tests whether the left and right values are not identical to one another (5 !== 2 + 3)
<
Less than: Tests whether the left value is smaller than the right one. (10 < 6)
>
Greater than: Tests whether the left value is greater than the right one.(10 > 20)
<=
Less than or equal to: Tests whether the left value is smaller than or equal to the right one. (3 <= 2)
>=
Greater than or equal to: Tests whether the left value is greater than or equal to the right one. (5 >= 4)
booleans data type
true/false values
operands
is what operators are applied to (5 * 2)
unary
An operator is unary if it has a single operand
the unary negation
- reverses the sign of a number
binary
An operator is binary if it has two operands.
String concatenation
Combines string with the binary operator (+) "m" + "s" if one operand is string end result will be string
Strings with other operators
converts them to numbers
The unary (+)
converts string to numbers
Unary position in pemdas
Higher than binaries
modify-and-assign
+= , etc.
postfix form
When the operator goes after the variable, counter++.
prefix form
when the operator goes before the variable: ++counter.
When to use prefix?
If we’d like to increase a value and immediately use the result of the operator
When to use postfix?
If we’d like to increment a value but use its previous value
JS coding style, readability
a style of “one line – one action”
comma operator
allows us to evaluate several expressions, dividing them with a comma ,. Each of them is evaluated but only the result of the last one is returned.
comma operator precedence
Very low, parentheses needed
Whats a string?
simply a piece of text surrounded by quotes
Method
a bit of functionality that is built into the language or into specific data types
Quotes
" '' " is good ' ' ' not good
Escape characters
(\) right before
A template literal
looks just like a normal string, but instead of using single or double quote marks (' or "), you use backtick characters (`): you can include variables in it, wrapped inside ${ }