If you are not in the tech world, you might have heard of the term “programming language”. So, what exactly is a programming language?
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. - Wikipedia
Javascript is one of the programming languages that allows you to implement complex features on web pages. Unlike Java, an object-based programming language that has the ability to run on a virtual machine and therefore can be run on any devices, Javascript is an object-oriented programming language that is more commonly used in web applications like browsers. That said, Javascript needs an interpreter to run and has to be embedded in the HTML of a web browser. It is a complementary language, which means you also need to know and understand HTML and CSS and how these work together with Javascript.
Think about a human, Javascript can be analogized as the brain, HTML as the body and CSS as clothing. While HTML provides the foundation, and content structure to a web page, CSS controls the styling and appearance of how these elements in the HTML should look, Javascript makes the page more alive and interactive by adding logics, interactivity and functionality to it. And just as a brain, Javascript interacts with HTML and CSS. It can change the HTML structure, or apply CSS styles dynamically, create animations, forms, etc based on user’s input or other events. The three layers build on top of one another nicely. Together, they form a powerful trio that enables the creation of dynamic and engaging web experiences.
The control flow is the order in which the computer executes statements in a script. Code is run in order from the first line in the file to the last line, unless the computer runs across the (extremely frequent) structures that change the control flow, such as conditionals and loops. A loop is a sequence of instructions that is repeated until a certain condition is met.
Let say you have a fixed timetable which you follow everyday. This lists all the tasks you do since you wake up until you go to bed. The control flow is the order in which you do these tasks in your timetable. In these tasks, there might be one that you will have to repeatedly do a sequences of smaller tasks until a certain condition is met. For example, when you cook dinner, you will need to cut vegetables, boil them, and then cut meat, fry them, and then again cut your fruit for dessert, etc until your meal is ready. This is an example of a loop. Until your dinner is ready, your cooking is not finished. You will need to repeat cutting, boiling (frying), plating, again and again.
Now lets look at an example of a for loop in action. Say you have an array variable names[] with 10 people’s name, and you want to log these onto the console. You could do it as below:
console.log(names[0]);
console.log(names[1]);
...
console.log(names[9]);
for(let i = 0; i < names.length; i++){
console.log(names[i]);
}
The Document Object Model, generally abbreviated to DOM, is the way through which JavaScript interacts with content within a website. Consider this HTML code:
<body>
<h1> Javascript Café </h1>
<div class="container">
<p id="whiteCoffee"></p>
<p>Some more text here!</p>
</div>
<script src="cafe.js"></script>
</body>
document.getElementById('whiteCoffee').innerHTML = 'White Coffee: ' + products.whiteCoffee.stock
In the previous example, we console.log the value of each elements in array names[]. So what exactly is an array?
An array is an ordered collection of values of any datatype, where each values is assigned a numeric index, start with 0 (e.g: first element is names[0] not names[1].
An object, on the other hand, is an unordered collection of key-value pairs, where each value is associated with a unique key.
We talked about how we could use Javascript to manipulate the DOM, including define and perform functions.
function plusTwo(num1,num2){
return num1+num2;
}
The function takes 2 parameters called num1 and num2, then return the specific value (num1+num2). So, why are they so important? Well, functions can be helpful in many ways when it comes to programming, as they enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.