Day 4 came up late because this was also the time that I moved into another country, home and job as well as I enrolled into my online bachelor degree. Today I finally found some time to start again my coding journey. So here it is, Day 4.
“The JavaScript engine actually compiles the program on the fly and then immediately runs the compiled code.”
“Another common usage of variables is for centralizing value setting. This is more typically called constants, when you declare a variable with a value and intend for that value to not change throughout the program.
const TAX_RATE = 0.08;”
“You’ll need the keyword
var
in every program, as it's the primary way you declare (aka create) _var_iables. “var a;” “
“the
log( b )
part is referred to as a function call. What's happening is we're handing theb
variable to that function… theconsole.
part is an object reference where thelog(..)
function is located.”
“Another way of creating output that you can see is to run an “alert( b );” statement. “
“using
console.log(..)
is generally going to make learning about coding and running your programs in the console easier than usingalert(..)
, because you can output many values at once without interrupting the browser interface.”
“there’s an easier way to get input. Use the “age = prompt( “Please tell me your age:” );” function.”
“Math:
+
(addition),-
(subtraction),*
(multiplication), and/
(division), as ina * 3
.”
“The
=
equals operator is used for assignment. as ina = 2
”
“Compound Assignment:
+=
,-=
,*=
, and/=
are compound operators that combine a math operation with assignment, as ina += 2
(same asa = a + 2
).”
“Increment/Decrement:
++
(increment),--
(decrement), as ina++
(similar toa = a + 1
).”
“Object Property Access:
.
as inconsole.log()
.”
“Equality:
==
(loose-equals),===
(strict-equals),!=
(loose not-equals),!==
(strict not-equals), as ina == b
.”
“Comparison:
<
(less than),>
(greater than),<=
(less than or loose-equals),>=
(greater than or loose-equals), as ina <= b
.”
“Logical:
&&
(and),||
(or), as ina || b
that selects eithera
orb
.”
“For much more detail, and coverage of operators not mentioned here, see the Mozilla Developer Network (MDN)’s “Expressions and Operators” (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators).”
“When you need to do math, you want a
"number”
.”
“When you need to print a value on the screen, you need a
"string"
(one or more characters, words, sentences).”
When you need to make a decision in your program, you need a
"boolean"
(true
orfalse
).
var a = "42";var b = Number( a );
// This is a single-line comment
/* But this is a multi line comment. */
“in code we often need to group a series of statements together, which we often call a block. In JavaScript, a block is defined by wrapping one or more statements inside a curly-brace pair
{ .. }"
if (..) { ..} elseif (..){
..
} else (..) {
..
}
while (..) {..}
do { ..} while (..);
“We can use JavaScript’s
break
statement to stop a loop.
while (true) { if (..) { break; } ..}
”
for (initialization clause; conditional test clause; update clause) { ..}
“Your program will almost certainly want to break up the code’s tasks into reusable pieces, instead of repeatedly repeating yourself repetitiously. The way to do this is to define a
function
.”
“A function is generally a named section of code that can be “called” by name, and the code inside it will be run each time. Consider:
function functionName() { ..;}
..
functionName();”
“Functions can optionally take arguments (aka parameters) — values you pass in. And they can also optionally return a value back.”
function functionName( value ) { ..;}
..
functionName( value );”
function functionName( value ) { .. return value;}
..
A = functionName( value );
“scope (technically called lexical scope). In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function’s scoped variables. A variable name has to be unique within the same scope — there can’t be two different
a
variables sitting right next to each other. But the same variable namea
could appear in different scopes. Also, a scope can be nested inside another scope.”
function outer() { ..
function inner() { ..
}
inner();
..}
outer();
prompt(..)
covered in "Input" earlier. You may prompt the user for their bank account balance, for example.Have fun and be creative!
This is where I, Demetris, stop. If you follow my journey considering taking the time to complete the above practice exercise before you go into the next “day”. In the beginning of the next day you will also get my own solution to this problem as well as the recommended solution.
Please do comment down below all your thoughts about my journey and If you find my posts here helpful to your own journey.
Thanks for being with me all this time.