JavaScript: Variables, Functions and Data Types!
Variables and functions in JavaScript are like the screws and bolts of coding. They’re the basic tools that can help developers get things done without getting too complicated.
Variables:
Think of variables as digital containers where we can stash all sorts of information for later. Whether it’s numbers, words, or more complex stuff, variables keep them safe and organized inside a computer’s memory. We give each container a name so that when we need the info again, we know exactly where to find it.
Naming variables in JavaScript isn’t too tricky. You should always start with a lowercase letter, avoid using spaces, and steer clear of any special words that JavaScript already knows. Following these rules keeps everything neat and easy to understand.
To set up a variable, we do two things: first, we declare it, and then we give it a value. This could be done using `let`, `const`, or `var`. It’s like saying, “Hey computer, I’ve got something to store here,” and then actually putting the stuff in the box.
Examples:
let age = 23
age is being declared using let, which gives us a number to represent someone’s age
const name = “Jay McCants”
The variable name is being declared using const that represents my name. Once assigned the value of ‘const’ the variable may not be changed.
var isStudent = true
The isStudent variable, declared with ‘var`. Unlike `const`, `var` allows redeclaration and reassignment, but it’s generally advised to use `let` or `const`.
These examples also have three primitive data types:
Number: an integer or floating point number. Example: 29
String: A series of characters that represent a textual value. Example: “Hello”
Boolean:A data types with two possible values: true and false
Data Types:
In JavaScript, there are eight data types outlined in the latest ECMAScript standard. They’re like building blocks for coding adventures, each with its own role. From the straightforward Booleans to the mysterious Symbols, they form the foundation of how we organize and handle data in our scripts.
When it comes to data types JavaScript is very flexible. You don’t need to declare them upfront, and they can change as your script runs. This flexibility lets us focus on writing code without worrying too much about data types, making our coding journey smoother and much easier.
Get Jay McCants’s stories in your inbox
Join Medium for free to get updates from this writer.
Functions:
Functions are like helpers that do specific tasks for us. Instead of writing out the same instructions repeatedly, we can bundle them into a function and just call it whenever necessary. It’s like having a recipe for making cookies, instead of explaining every step each time, we can just say, “Make cookies!” and the function takes care of the rest.
Something interesting about functions is that they can be flexible. We can give them inputs, called parameters, so they can do slightly different things depending on what we need. And when we want them to actually do their job, we just call them by name, and they jump into action.
Examples:
function add(a,b) {
return a + b;
}
function subtract (a, b) {
return a — b;
}
function multiple (a, b) {
return a * b;
}
These functions perform basic math operations and return results
Conclusion:
In the world of JavaScript, variables and functions are like the tools in a toolbox. They’re very essential for getting stuff done. Whether it’s storing data in variables or automating tasks with functions, mastering these basics sets the foundation for building all sorts of cool stuff in the digital world. So next time you’re writing code, remember: keep it simple, use your variables wisely, and let your functions do the heavy lifting!
Resources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function
https://learning.flatironschool.com/courses/7657/pages/javascript-variables?module_item_id=676813




