DevX JS - W5C13 - Modern Syntax.pptx
DevX JS - W5C13 - Modern Syntax.pptx
Modern Syntax
Review
Review
● jQuery ● jQuery
○ Libraries ○ setters/getters & set/get in CSS
○ Importing jQuery ○ Display and Fade
○ jQuery, jQuery vs Vanilla JS ○ Event Listeners
○ jQuery syntax and $(selector) ○ AJAX
○ method chains ○ Refactoring
○ html/text/val methods
Modern Syntax
Modern Syntax
There are many ways to write code.
As a beginner I recommend that you learn one way to do things, and learn it well. Once you
understand one way to write code, you can start looking into different or better ways to do it.
This lesson will cover some of those other ways of writing code. You don’t need to master
them now! You should be familiar with these before working professionally, as you may see
code written in many different ways..
For the remainder of the course, feel free to use them. If you are comfortable with one way of
writing your code, try using this other styles.
ES2020
ES2020
ES stands for Ecma Script. This is a name given to a Javascript standard. JS is used by many
browsers and other environments, and they must be kept to ES standard. This allows for JS to
be consistent everywhere.
Standards change in small ways every year or two years. Some of those changes are small but
insignificant, others are small but significant. It is wise to pay attention to these changes.
More often new core functions are added for data types.
Var?
const arr = [1, 2, 3, 4, 5];
Var?
const arr2 = [6, 7, 8, 9, 10];
const bothArr = [...arr, ...arr2];
Const
const arr2 = [6, 7, 8, 9, 10];
const bothArr = [...arr, ...arr2];
‘Let’ differs from ‘var’ in a very subtle way: for(let y = 0; y < 3; y++){
‘Let’s declaration is “block scoped”. setTimeout(() => {
console.log(y)
In the above example, “var” x gets read 1
second later, and each timeout see’s the }, 2000);
LAST value of x… 3. }
Spread …
var arr2 = [6, 7, 8, 9, 10];
var bothArr = [...arr, ...arr2];
Spread Operator
var berries = ["grape", "cherry",
"raspberry"];
var ingredientsForDinner;
console.log(ingredientsForDinner);
Spread with
Functions
Spread With Functions
Functions can also use the spread (...)
operator. function manyArgs(...args) {
console.log(args);
Spread operators can go in function }
parameters. But they only work as the last
parameter. manyArgs(1, 2, 3, 1, 2, 3);
When used, all arguments after the defined function makeClass(teacher, ...students) {
ones will be placed in that parameter, as an console.log(`${teacher} says hi to
array. ${students.join(" and ")}`);
}
There are many ways to write the same code. JS has multiple ways of expressing the same ideas.
var a = 5; var a = 5, b = 6;
var b = 6; if(a < b) console.log("banana");
if(a < b){
console.log("banana")
}
Shorthand Logic
Here we see two things: variable declaration with commas, and logical operation without curly
braces.
And provided your IF-statement has only one line of code to run, you don’t need curly braces.
(But you do need a line break.)
var a = 5; var a = 5, b = 6;
var b = 6; if(a < b) console.log("banana");
if(a < b){
console.log("banana")
}
Arrow Functions
Arrow Functions
There are 5 (!) ways to write a function in JS.
function double(x){
When we first studied functions we learned return x*2
one way. Now we are more familiar with
}
functions. So we’ll learn the other ways.
var double = function(x){
I would recommend writing out a full function when writing functions with a complex purpose,
or more than 3 lines of code.
var testScores = [12, 15, 30, 55, 60, 76, 88, 90, 100, 95, 45, 76, 82];
var passingScores = testScores.filter((s) => s > 50);
console.log(passingScores);
Function Details
Declaration Expression
function double(x){return x*2} var double = function(x){return x * 2}
Some of these details may be unclear now. Don’t worry, it will become clear later, once you get
more familiar with the internal logic of JS.
Basically, “function” and “=>” are mostly similar, but have slight different operations. There are
a few scenarios where you cannot use “=>” instead of “function”.
And / Or
And / Or
These operators: “&&” and “||” are
known as “and” and “or”. true && “A” = “A”
They are used in logical operations,
such as if-statements.
false && “A” = false
(But technically something else is
happening behind the scenes. The
operator looks at the prior value, and true || “A” = true
then either evaluates TO that value, or
to the NEXT value.)
|| wants to succeed!
It will give you true when it finds it.
Object Property
Assignment
Object Property Assignment
There’s also a shorthand for object property
assignment. If you want to assign a variable to var name = "Zintis";
a property that has the same name, you can var location = "USA";
just write the variable name. See (1). var role = "teacher";
You can study this more later. For now, this brief overview is enough.