While HTML and CSS provide website structure, JavaScript brings interactivity to websites by enabling complex functionality, such as handling user input, animating elements, and even building full web applications.
Data types help the program understand the kind of data it’s working with, whether it’s a number, text, or something else.
Number Type: A number represents both integers and floating-point values. Examples of integers include 7, 19, and 90.
Floating point: A floating point number is a number with a decimal point. Examples include 3.14, 0.5, and 0.0001.
String: A string is a sequence of characters, or text, enclosed in quotes. "I like coding" and 'JavaScript is fun' are examples of strings.
Booleans: A boolean represents one of two possible values: true or false. You can use a boolean to represent a condition, such as isLoggedin = true.
Undefined & Null: An undefined value is a variable that has been declared but not assigned a value. A null value is an empty value, or a variable that has intentionally been assigned a value of null.
Object: An object is a collection of key-value pairs. The key is the property name, and the value is the property value.
Symbol: The Symbol data type is a unique and immutable value that may be used as an identifier for object properties.
BigInt: When the number is too large for the Number data type, you can use the BigInt data type to represent integers of arbitrary length. By adding an n to the end of the number, you can create a BigInt.
Strings are sequences of characters enclosed in quotes. They can be created using single quotes and double quotes.
Strings are immutable in JavaScript. This means that once a string is created, you cannot change the characters in the string. However, you can still reassign strings to a new value.
String Concatenation in JavaScript
Concatenation is the process of joining multiple strings or combining strings with variables that hold text. The + operator is one of the simplest and most frequently used methods to concatenate strings.
let studentName = "Asad";let studentAge = 25;let studentInfo = studentName + " is " + studentAge + " years old.";console.log(studentInfo); // Asad is 25 years old.
If you need to add or append to an existing string, then you can use the += operator. This is helpful when you want to build upon a string by adding more text to it over time.
Another way you can concatenate strings is to use the concat() method. This method joins two or more strings together.
let firstName = "John";let lastName = "Doe";let fullName = firstName.concat(" ", lastName);console.log(fullName); // John Doe
Logging Messages with console.log()
The console.log() method is used to log messages to the console. It’s a helpful tool for debugging and testing your code.
JavaScript as a Dynamically Typed Language
JavaScript is a dynamically typed language, which means that you don’t have to specify the data type of a variable when you declare it. The JavaScript engine automatically determines the data type based on the value assigned to the variable.
let error = 404; // JavaScript treats error as a numbererror = "Not Found"; // JavaScript now treats error as a string
Using the typeof Operator
The typeof operator is used to check the data type of a variable. It returns a string indicating the type of the variable.
let age = 25;console.log(typeof age); // "number"let isLoggedin = true;console.log(typeof isLoggedin); // "boolean"
However, there’s a well-known quirk in JavaScript when it comes to null. The typeof operator returns "object" for null values.
let user = null;console.log(typeof user); // "object"
In JavaScript, functions and object methods are both ways to encapsulate reusable code, but they have some key differences in how they are defined, used, and the context in which they operate. Understanding these differences is crucial for writing effective and organized JavaScript code.
Difference Between Functions and Object Methods