How to JavaScript Part 1

Libor Marko
3 min readJun 1, 2019

--

I have been lately teaching myself how to code in JavaScript. I already know many subjects, which are very similar or the same as in python, which fastens my improvement. Here is a summary of my progress, I picked one example to represent each section I finished.

What I learned:

  • Basic JavaScript
  • ES6
  • Regular Expressions

Basic JavaScript

Basic JavaScript section example

This example is about a modification of a given album collection. The collection is represented as a JSON object. I created the function updateRecords that takes an album’s id, a property such as “album”, and value such as “Let it Rock”, and modifies the data in the collection. I used if, else, and else if statements to handle rules for the particular combinations of parameters. Depending on the parameter combination, the function updates or sets a specified value of a parameter. If a value is an empty string, the function deletes a property from the object. The function always returns the entire collection object.

ES 6

ES6 section example

I worked with getter and setter in this example. Getter and setter control attributes of an object from outside. Getter allows to access properties and setter allows to change or mutate them. I created the class Thermostat and put constructor, getter, and setter in there. Now I can create a new instance of the class and set its initial value to a temperature in the Fahrenheit temperature scale. Following get method allows to convert it to a temperature in the Celsius temperature scale. The set method allows defining a new temperature in the Celsius temperature scale.

Regular Expressions

Regular Expressions section example

I used regular expressions to check all the usernames in a database. A variable userCheck is created to check a few rules, which need to be followed while creating a username. Users can use zero or more numbers in the username, and they have to be located at the end (expressed as \d*$;). Username letters can be lowercase and uppercase (expressed as i). Username has to be at least two characters long. If a username has only two letters, it can use only alphabet letter characters (expressed as ^[a-z]{2,}). I used the test method to take the regex, apply it to a string and return true or false if the pattern finds something or not. In our case, the test method returned true as “JellyBelly” satisfies all conditions.

--

--

Libor Marko

Senior FrontEnd Engineer. Follow me on LinkedIn or check my web page: http://libormarko.github.io