JavaScript is a dynamic programming language that is used web development. There are two ways in which you can execute to code in browser.
Adding JavaScript code in <script>
tags in your html file
<input type="text" onkeydown="logKeyPresses(this)">
<script>
function logKeyPresses(e) {
console.log(e.key);
}
</script>
Making a seperate JavaScript file with .js
extension and importing the code in your html file
<!-- index.html -->
<input type="text" onkeydown="logKeyPresses(this)">
<script src="main.js"></script>
// main.js
function logKeyPresses(e) {
console.log(e.key);
}
NOTE: Both the examples given above have the same output
Let's take a look at the concepts that will be covered in this devlog.
Variables and constants are simply name of storage locations, the basic difference between them is that variables can change while the program is being executed. Whereas, constants remains the same and they need to be initialized not declared.
When you declare a variable it tells the compiler about the existence of the entity in the program and its location but it does not contain a value. Whereas, initializing a variable means declaring a variable and also assigning a value to it.
As mentioned above, javascript is dynamically typed which means you don't need to specify types. You might be wondering, how many data types javascript has and what are they?.
Intotal, there are 8 data types.
Variables are made using 2 keywords in JavaScript.
let
var
During the early times of JavaScript, there was only one method to declare a entity that maps to a location in the memory. During that time you could not declare a constant the only option was to declare a variable using var keyword.
This introduced a lot of bugs into the code and lead to unexpected behaviours. That's why let and const keywords were added. Now you why let and const were added to JavaScript.
var
and let
?The similarity between var and let is that they are used to declare a variable. With var keyword you can declare same variable twice and it always declares variable in global scope.
Whereas, with let you can only decalare variable once but you can reassign the value to it n number of times. It also respects scopes.
What I meant is that if I declared a variable in a function using let it will not be accesable outside it but if was declared using var keyword then I would be able to access it outside function.
Declare a variable using var keyword.
var x = 5;
var y = 5;
var z = x + y;
// Changing the value of z
{
var z;
z = 15;
}
// Print the variable to the console
console.log(z); // 15
Declaring a variable using let keyword
let x = 5;
let y = 5;
let z = x + y;
// Changing the value of z
{
let z;
z = 15
}
// Print the variable to the console
console.log(z); // 10
let x = 50;
let y = 6;
// Addition
console.log(x + y); // 56
// Substraction
console.log(x - y); // 44
// Multiplication
console.log(x * y); // 300
// Division
console.log(x / y); // 8.334
// Remainder -> by using modulus operator (%)
console.log(x % y); // 6.0
Copyright © 2023. codelog.dev