Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
0%
Lesson 4.3 JavaScript Strings
Strings in Javascript
- Strings are primitive data types/ immutable
- parsed: data is stored directly in a variable (for basic storage)
- this means they are unchanging
- don’t have properties and types
- formatted similar to python, using quotes (both single and double) and starting at 0
%%js
console.log("simple way to display strings");
//console.log prints in console
//js lingo: what's inside source code is called string literal. What appears in the console is called String value.
//use variable to assign values of string
//variables are better bc you can have spaces without a plus sign
const stringExample = "another example of printing a string";
console.log(stringExample);
//if string has an apostrophe and you're using single quotes, use backslash to negate apostrophe (same as python)
//you can also switch to double quotes
console.log("the dog said \"where is my food\" as the owner left");
//concat
const mascot = "night" + "hawk";
console.log(mascot);
//you can concat multiple variables together under a separate variable similar to in python
const fish1 = "pufferfish";
const fish2 = "marlin";
const fish3 = "salmon";
const favFish = "My favorite fish species are" + fish1 + ", " + fish2 + ", and" + fish3 ".";
console.log(favFish);
//template literal, uses backticks instead of quotes
//more convienent way to concat with diff syntax, not as messy
const favFishMethodTwo = `My favorite fish species are ${fish1}, ${fish2}, and ${fish3}.`;
console.log(favFishMethodTwo);
//can also be used to define strings without the worry of overlapping quotes
console.log(`I'm sure this is "correct"`);
//new lines with \n
const riddle = "my favorite animal\nhiberates during the winter\nin a cave";
console.log(riddle);
//for template literal, enter string in new line
const threeLines = `this is line one
this is line 2
this is line 3`;
console.log(threeLines);
<IPython.core.display.Javascript object>