...

/

Variables, Built-In Data Types, Operators, and Arithmetic Expressions

Variables, Built-In Data Types, Operators, and Arithmetic Expressions

Learn the essentials of JavaScript, including basic data types, variables, arithmetic operations, and string concatenation with comparisons to Python.

Understanding key data types, variables, operators, arithmetic expressions, and string concatenation is crucial for building and manipulating data in any programming language. This lesson will compare these concepts in Python and JavaScript.


Variables

Variables in any language are fundamental building blocks that allow us to store and manipulate data in our programs. They act as containers for storing values, which can be numbers, strings, objects, or other data types.

Python uses dynamic typing. Variables can be declared and initialized without specifying the data type.

age = 25
name = "Mike"
Variables in Python

Variables in JavaScript, just like Python, are dynamically typed and can be declared and initialized without specifying the data type.

In contrast with Python, no initial value needs to be assigned at the time of variable declaration.

Variables can be declared using three keywords: var, let, and const.

  • var: The var keyword has been traditionally used in JavaScript to declare variables.

Press + to interact
var name; // Variable declared without assigning value
var age = 30;
name="David"; // Value assigned to the 'name' variable
console.log(name);
console.log(age);

However, var has some quirks, such as function-scoping, which can lead to unexpected behavior in certain cases.

Press + to interact
function showVar() {
var x = 10;
console.log(x); // Outputs: 10
}
showVar();
console.log(x); // ReferenceError: x is not defined

var is function-scoped, meaning x is only accessible inside the showVar function. Trying to access x outside the function results in a ReferenceError, because it’s not available globally.

  • let: Introduced in ES6 (ECMAScript 2015), let is now the preferred way to declare variables because it is block-scoped, meaning it is only accessible within the block where it is defined.

Press + to interact
let greeting = "Hello";
let number = 10;
if (number > 5) {
let message = "Number is greater than 5";
console.log(message); // Output: Number is greater than 5
}
// console.log(message); // Uncaught ReferenceError: message is not defined

Note: If you uncomment line 7, you would see the output as Uncaught ReferenceError: message is not defined. This is due to the type of message variable, which is let and is not accessible outstide its scope.

  • const: This was also introduced in ES6. const is used to declare variables that are intended to be constant, meaning their value cannot be reassigned after being initialized. However, if the variable holds an object or array, its properties or elements can still be modified.

Press + to interact
const pi = 3.14;
console.log(pi); // Output: 3.14
const person = { name: "David", age: 25 };
person.age = 26; // This is allowed
console.log(person.age); // Output: 26
// person = { name: "Charlie", age: 28 }; // Uncaught TypeError: Assignment to constant variable.

Note: ...