JS 1: variables, functions, parameters, array methods, data types, Math operations and HTML implementation

Var, const, and functions.

Javascript is an Object-Oriented language based on Prototypes (not on classes )

var, const, and let are keywords used to store data:

let was introduced in 2015 and declares a block-scoped, local variable.

//The assign operator = stores the value, we repeat the assign to edit it
let uno = 'coming'
uno = 'The javascript is ' + uno

console.log(uno)        //"The javascript is coming"

//multiple assigns
let primo= 1, secondo= 2, terzo= 3;

//ALSO, we can only use _ or - on let names

We can create multi-line strings using ' ' and /n.

let poetry = `
Nel mezzo del cammin di nostra vita \n
mi ritrovai per una selva oscura, \n `

console.log( poetry )        //Nel mezzo del cammin di nostra vita
                             //mi ritrovai per una selva oscura,
chevron-rightCode Blocks and function/block scope in variableshashtag

JavaScript blocks are code sections enclosed in curly brackets {}, and functions are a type of block that can be invoked.

  • The let variable is block-scoped, it's only accessible within the block it's declared in.

  • The var variable is function-scoped, if declared in a block that's not a function, it becomes part of the global object, making it accessible anywhere.

Functions are blocks of code used to perform tasks when invoked:

you can use the function keyword and a reference name to create a function:

return will stop execution and return values, there are no implicit returns in javascript.

In Javascript, all functions are object methods, be it from constructor or global object.

Parameters are values that are passed to the function and can be used locally in the function:

Arguments are the actual values passed when invoking the function:

Functions too can be used as parameters, check Function composition to know more.

We Interpolate variables into HTML elements using Ternary Operators:

We use template strings (template iterals) to interpolate variables and expressions into a string, including Ternary Operators

chevron-rightHow to enable the backtick on the keyboardhashtag

Depending on your language your keyboard might not have the backtick (` `).

Download the developer's keyboardarrow-up-right, and change the keyboard layout by pressing: windowsIcon + spacebar, use space to change the layout.

It will become avaiable using Alt Gr + '

The console is where we run scripts and commands.

Math Operations, Booleans, and Arrays methods.

Most Math operations work the same in Javascript +, -, *, and / following the Pendas priority system, we use == for comparisons (while = is to assign variables values).

We can also perform Math.___() operations, Math is a Global Built-In Object for Mathematics:

The NaN (not a number) operations are as such:

Arrays are objects variables that can hold more than one value and one typeof() data:

We can check its built-in methods and properties in its [[prototype]] property, like concat() or .length:

Methods on the other hand are actions on objects, and they need () because they are function:

chevron-rightArray push(), pop(), shift(), unshift(),fill() and splice() methods guidehashtag

We can use methods on strings, and we can chain them:

For arrays, we can use push(), pop(), shift() and unshift():

Similar to splice() the fill() method uses array indexes to overwrite the array.

We use .splice( [starting array index ], cut elements number, elements added ) to substitute multiple array elements, remember that the starting index is included in the splice.

Other methods like .includes() and .startsWith() will return true/false:

We can use typeof to check data type.

Pop() and Shift() can both store values and return methods:

On arrays, we can .sort() and .reverse() the array elements order, and also use .from() to make strings into arrays:

Any function passed as an argument, inside a method, it's a callback function:

Booleans are a type of data and we obtain it after logical operators:

Booleans, Strings, and Numbers are the most known primitives, contrary to objects, which are aggregations of properties, primitives are just values, they have no property:

Data conversion and type helpers

We can use the built-in helpers Boolean(), String() Number() to check/convert to respective data types:

We can also have data coercion where values are converter automatically :

Js in HTML implementation

To link our external .js file to the HTML page we use:

We use createElement(), createTextNode() and appendChild() to potray JS content into HTML :

How to use variables and arrays in the DOM:

JS in DOM with arrays and event

Check this exercise:

JS on DOM

We use Math.floor() to get random 2-integers numbers:

We use an external array to store the random numbers, while also resetting the innerHTML:

We also wanted the numbers and the sum to happen separately:

To show the arrays with the stored values we:

Last updated