Javascript day 1

Intro

I'm learning through this course by Jad Joubran.

Before starting over, I've finished 58 out of 83 lessons but hardly remember anything. Let's dive into this again!

This course teaches JavaScript in the context of building web applications.

Prerequisites for this course are:

  • Variables
  • Arrays and array iteration (loops)
  • Conditions
  • Objects
  • Basics of Classes
  • HTML / CSS

He encourages learners to make a lot of mistakes!

Basic functions

function sum(x, y) {
  return x + y;
  console.log("Hello world"); // this will NEVER run
}

Note:

  • In JS, you need to return from inside the function. If not, your function will return undefined.
  • The return keyword will exit the function.
  • Anything after return will never run.

Strings

Create strings using double or single quotes.

"Walnut is awesome";
'Awesome Walnut'

String properties

.length returns the length of the string.

// It works with both cases
console.log("Walnut is awesome".length);
// or
let text = "Awesome Walnut";
console.log(text.length);

Basic string methods

Common methods that can be called on strings:

.toLowerCase();

.toUpperCase();

These methods return new strings, and you can guess their purpose from their names.

Visualize a variable (or expression)

Use console.log to visualize a variable or even an expression.

function sum(a, b) {
  console.log(a);
  console.log(a + b);
  return a + b;
}

Come back and answer these questions later.

  • Make sure you understand the reserved keywords in JS.
  • What is a parameter?
  • Check the difference between a method and a property:
    In JavaScript, methods and properties are both members of objects, but they serve different purposes:
    • Properties
      • Definition: A property is a variable that belongs to an object.
      • Stores: Data or values (like strings, numbers, arrays, or even functions).
      • Syntax: object.property
    • Methods
      • Definition: A method is a function that belongs to an object.
      • Does: Performs an action or behavior.
      • Syntax: object.method()
  • What is the best online tool to test my JS code outside the course?
    • Codepen
    • Codesandbox
    • Simple local files